CocoaPods trunk is moving to be read-only. Read more on the blog, there are 17 months to go.
TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Sep 2015 |
SPMSupports SPM | ✗ |
Maintained by Lucas Ihlström, Lucas Ihlström.
CleanNotification is an attempt to make notifications more sensible in Swift.
Register for a notification:
DogDidBarkNotification.register(self) {
dog, bark in
println("\(dog.name) barked with decibel: \(bark.decibel) at target: \(bark.target)")
}
Or with the custom syntax:
DogDidBarkNotification <<- self - {
dog, bark in
println("\(dog.name) barked with decibel: \(bark.decibel) at target: \(bark.target)")
}
You can also register anonymously:
//Since we're not supplying a token, we cannot unregister later on.
DogDidBarkNotification <<- {
dog, bark in
println("\(dog.name) barked with decibel: \(bark.decibel) at target: \(bark.target)")
}
Post the notification:
didBarkProducer.produce(self, value: Bark(decibel: 80.0, target: "John Doe"))
Or with the custom syntax:
didBarkProducer - self ->> Bark(decibel: 80.0, target: "John Doe")
Unregister from a notification:
DogDidBarkNotification.unregister(self)
Or with the custom syntax:
DogDidBarkNotification -= self
Define a notification as follows:
private let didBarkProducer = NotificationProducer<Dog, Bark>()
let DogDidBarkNotification = Notification<Dog, Bark>(producer: didBarkProducer)
The first generic argument is the sender of the notification (in this case Dog), the second (in this case Bark) is the notification value, think of it as the userInfo.
The producer is what allows the Dog class to be the sole poster of the notification. But if you want any file, anywhere, to be able to post the notification, use an OpenNotification:
let DogDidBarkNotification = OpenNotification<Dog, Bark>()
If you want to change what thread the notification is delivered on, supply it it in the constructor:
let DogDidBarkNotification = Notification<Dog, Bark>(producer: didBarkProducer, deliveryPolicy: .SenderThread)
Available delivery policies:
public enum ThreadDeliveryPolicy {
case MainThread
case SenderThread
case CustomDispatchQueue(dispatch_queue_t)
}
struct Bark {
let decibel: Double
let target: String
}
private let didBarkProducer = NotificationProducer<Dog, Bark>()
let DogDidBarkNotification = Notification<Dog, Bark>(producer: didBarkProducer)
class Dog {
let name: String
init(name: String) {
self.name = name
}
func bark(target: String) {
didBarkProducer - self ->> Bark(decibel: 80.0, target: target)
}
}
@IBAction func buttonPressed(sender: UIButton) {
let dog = Dog("Buddy")
dog.bark("John Doe")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
DogDidBarkNotification <<- self - {
dog, bark in
println("\(dog.name) barked with decibel: \(bark.decibel) at target: \(bark.target)")
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
DogDidBarkNotification -= self
}
CleanNotification is released under the MIT license. See LICENSE for details.