Keep Your Naming Conventions:
This library gives you convenient access toNotificationCenter, but it's up to you to set it up the way you like!Nothing to Hide:
Not trying to hideNotificationCenterfunctionality. Just an attempt to provide a more convenient APIFull and Simple Testing:
Testing this library was simple, since it only forwards calls toNotificationCenterfor the most part. Mocking that object allowed tests to reach 100% coverage.
You can try them in the playground shipped with the framework!
Use your own naming convention to wrap NotificationCenter
letnsCenter=NotificationCenter.default let📡=NotificationCenterAdapter(notificationCenter: nsCenter) 📡.post(.💃) // my personal preference, define this in Globals.swift letNC=NotificationCenterAdapter(notificationCenter: nsCenter) // Now, you can use `NC` throughout the appFour simple keywords to remember
letobj=Object()NC.add(obj, selector: #selector(Object.call)) // normal add observer NC.observe{ notification in} // observe using blocks NC.post(.tenhut) // post a notification NC.remove(obj) // remove from nsCenterTransparent and convenient API
letkeys=["observe","many","keys"].map{Notification.Name($0)}NC.observe(keys){ _ in} // observe on the same thread NC.observeUI(keys){ _ in} // delivered to the main thread NC.post(.test)NC.post(.test, userInfo:["info":5])NC.post(Notification(name:.test, object:nil))RAII-based observers
classDummy{ // declare the observer as optional varbroadcastObserver:Observer?init(){ // assign it anywhere you like broadcastObserver =NC.observe{[unowned self] _ inself.doSomething()}.execute() // this is a neat bonus feature }func doSomething(){ // exectued twice, since we call "execute" manually print("it works!")}}vardummy:Dummy?=Dummy()NC.post(.test) // calls doSomething dummy =nil // clean up is automatic NC.post(.test) // doesn't crash!Carthage is fully supported. Simply add the following line to your Cartfile:
github"SwiftKitz/Notificationz"Cocoapods is fully supported. Simply add the following line to your Podfile:
pod'Notificationz'For manual installation, you can grab the source directly or through git submodules, then simply:
- Drop the
Notificationz.xcodeprojfile as a subproject (make sureCopy resourcesis not enabled) - Navigate to your root project settings. Under "Embedded Binaries", click the "+" button and select the
Notificationz.framework
After migrating to Swift, the NotificationCenter APIs really stood out in the code. Writing so much boiler plate all over the place just to register, handle, and cleanup notifications. Coming from C++, RAII seemed a pretty invaluable pattern to be applied here.
With this framework, one can easily declare all their observers as properties:
classSample{privatevarkeyboardObserver:Observer?privatevarreachabilityObserver:Observer?}Other programmers should be pleased with this consistency! Moreover, there is no need to worry handling notifications in some other function somewhere nor do cleanup in deinit. It just works:
keyboardObserver =NC.observeUI(UIKeyboardWillShowNotification){[unowned self] _ in // you can write your handler code here, maybe call another function } // you can force cleanup by setting the property to nil, but don't have to keyboardObserver =nilMaz (@Mazyod)
Notificationz is released under the MIT license. See LICENSE for details.