Shouter is a simple, safe, lightweight way for one-to-many communication. It is a type, memory and thread safe alternative for NotificationCenter.
- Type Safe: No more
userInfo: [String: Any]dictionary guarded casting. - Thread Safe: You can
register,notify,unregisterin any thread without crash and data corruption. - Memory Safe:
Shouterstores observers as a zeroing-weak reference by usingNSHashTable. No crash and no need tounregistermanually before deallocating.
Define a protocol, that your observer implements:
protocolSomeNotification{func somethingHappened(value:String)}classViewController:UIViewController{ /* ... */ }extensionViewController:SomeNotification{func somethingHappened(value:String){self.titleLabel.text ="Something happened: \(value)"}}Have an observer:
letvc=ViewController()Register the observer for the notification:
Shouter.default.register(SomeNotification.self, observer: vc)Notify all observers:
Shouter.default.notify(SomeNotification.self){
$0.somethingHappened(value:"Hello World")}Unregister, when the observer is not interested in the notification anymore:
Shouter.default.unregister(SomeNotification.self, observer: vc)CocoaPods:
pod 'Shouter'
Swift Package Manager:
.package(url:"https://github.com/ChaosCoder/Shouter.git", from:"0.5.0")Shouter was inspired and partially based on the library 100mango/SwiftNotificationCenter.
The logo is based on the "Broadcast" icon by Amy Chiang from the Noun Project.
