SwiftNotificationCenter

所属分类:C/C++基础
开发工具:Swift
文件大小:0KB
下载次数:0
上传日期:2019-06-11 09:07:20
上 传 者sh-1993
说明:  面向协议的通知中心,具有类型安全、线程安全和内存安全性
(A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety)

文件列表:
Example/ (0, 2019-06-11)
Example/Example.xcodeproj/ (0, 2019-06-11)
Example/Example.xcodeproj/project.pbxproj (15340, 2019-06-11)
Example/Example.xcodeproj/project.xcworkspace/ (0, 2019-06-11)
Example/Example.xcodeproj/project.xcworkspace/contents.xcworkspacedata (152, 2019-06-11)
Example/Example/ (0, 2019-06-11)
Example/Example/AppDelegate.swift (2070, 2019-06-11)
Example/Example/Assets.xcassets/ (0, 2019-06-11)
Example/Example/Assets.xcassets/AppIcon.appiconset/ (0, 2019-06-11)
Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json (1077, 2019-06-11)
Example/Example/Assets.xcassets/first.imageset/ (0, 2019-06-11)
Example/Example/Assets.xcassets/first.imageset/Contents.json (154, 2019-06-11)
Example/Example/Assets.xcassets/first.imageset/first.pdf (2465, 2019-06-11)
Example/Example/Assets.xcassets/second.imageset/ (0, 2019-06-11)
Example/Example/Assets.xcassets/second.imageset/Contents.json (155, 2019-06-11)
Example/Example/Assets.xcassets/second.imageset/second.pdf (2423, 2019-06-11)
Example/Example/Base.lproj/ (0, 2019-06-11)
Example/Example/Base.lproj/LaunchScreen.storyboard (1740, 2019-06-11)
Example/Example/Example-Bridging-Header.h (104, 2019-06-11)
Example/Example/FirstViewController.swift (1019, 2019-06-11)
Example/Example/Info.plist (1641, 2019-06-11)
Example/Example/Main.storyboard (12741, 2019-06-11)
Example/Example/NotificationProtocol.swift (1389, 2019-06-11)
Example/Example/SecondViewController.swift (503, 2019-06-11)
Example/Example/ThirdViewController.swift (508, 2019-06-11)
LICENSE.txt (1075, 2019-06-11)
SwiftNotificationCenter.podspec (649, 2019-06-11)
SwiftNotificationCenter.xcodeproj/ (0, 2019-06-11)
SwiftNotificationCenter.xcodeproj/project.pbxproj (16950, 2019-06-11)
SwiftNotificationCenter.xcodeproj/project.xcworkspace/ (0, 2019-06-11)
SwiftNotificationCenter.xcodeproj/project.xcworkspace/contents.xcworkspacedata (168, 2019-06-11)
SwiftNotificationCenter.xcodeproj/xcshareddata/ (0, 2019-06-11)
SwiftNotificationCenter.xcodeproj/xcshareddata/xcschemes/ (0, 2019-06-11)
SwiftNotificationCenter.xcodeproj/xcshareddata/xcschemes/SwiftNotificationCenter.xcscheme (3883, 2019-06-11)
SwiftNotificationCenter/ (0, 2019-06-11)
SwiftNotificationCenter/Info.plist (806, 2019-06-11)
SwiftNotificationCenter/SwiftNotificationCenter.h (584, 2019-06-11)
SwiftNotificationCenter/SwiftNotificationCenter.swift (2499, 2019-06-11)
... ...


![](http://img.shields.io/badge/Swift-5.0-blue.svg) A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety. - Type Safe No more `userInfo` dictionary and Downcasting, just deliver the concrete type value to the observer. - Thread Safe You can `register`, `notify`, `unregister` in any thread without crash and data corruption. - Memory Safety `SwiftNotificationCenter` store the observer as a zeroing-weak reference. No crash and no need to `unregister` manually. It's simple, safe, lightweight and easy to use for `one-to-many` communication. ## Usage Define protocol and observer: ~~~swift protocol Update { func updateTitle(title: String) } extension ViewController: Update { func updateTitle(title: String) { self.titleLabel.text = title } } let vc = ViewController() ~~~ Register: ~~~swift Broadcaster.register(Update.self, observer: vc) ~~~ Broadcast: ~~~swift Broadcaster.notify(Update.self) { $0.updateTitle("new title") } ~~~ Unregister: ~~~swift Broadcaster.unregister(Update.self, observer: self) ~~~
Compare with `NSNotificationCenter` : For example, handle `UIKeyboardWillShowNotification` ~~~swift @objc func handleKeyboardNotification(notification: NSNotification) { guard notification.name == NSNotification.Name.UIKeyboardWillShow else { return } guard let beginFrame = (notification .userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return } guard let endFrame = (notification .userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return } // use beginFrame, endFrame } ~~~ `SwiftNotificationCenter` way: ~~~swift /* If you want to observe the system built in notifications like this. You can declare a protocol and the relevant method, and use a singleton as a mediator to observe system's notification, then notify our observers. Please check the refactor example in SwiftNotificationCenterExample Project. */ func UIKeyboardWillShow(beginFrame: CGRect, endFrame: CGRect) { } ~~~ ## Installation CocoaPods: ~~~ pod 'SwiftNotificationCenter' ~~~ Carthage: ~~~ github "100mango/SwiftNotificationCenter" ~~~ Manually: Just copy source files in the SwiftNotificationCenter folder into your project. ## License `SwiftNotificationCenter` is under the MIT license.


近期下载者

相关文件


收藏者