TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jun 2019 |
SPMSupports SPM | ✗ |
Maintained by mazyod.
Keep Your Naming Conventions:
This library gives you convenient access to NotificationCenter
, but it’s up to you to set it up the way you like!
Nothing to Hide:
Not trying to hide NotificationCenter
functionality. Just an attempt to provide a more convenient API
Full and Simple Testing:
Testing this library was simple, since it only forwards calls to NotificationCenter
for 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
let nsCenter = NotificationCenter.defaultCenter()
let 📡 = NotificationCenterAdapter(notificationCenter: nsCenter)
📡.post("💃")
// my personal preference, define this in Globals.swift
let NC = NotificationCenterAdapter(notificationCenter: nsCenter)
// Now, you can use `NC` throughout the app
Four simple keywords to remember
NC.add(obj, selector: Selector("call:")) // normal add observer
NC.observe { notification in } // observe using blocks
// it's recommended you define your notifications as enums
let name = Notification.Name(rawValue: "Ten-hut!")
NC.post(name) // post a notification
NC.remove(obj) // remove from nsCenter
Transparent and convenient API
let keys = ["observe", "many", "keys"].map {
Notification.Name(rawValue: $0)
}
NC.observe(keys) { _ in } // observe on the same thread
NC.observeUI(keys) { _ in } // delivered to the main thread
NC.post(notificationName)
NC.post(anotherName, userInfo: ["info":5])
NC.post(Notification(name: differentName, object: nil))
RAII-based observers
class Dummy {
// declare the observer as optional
var broadcastObserver: Observer?
init() {
// assign it anywhere you like
broadcastObserver = NC.observe { [unowned self] _ in
self.doSomething()
}.execute() // this is a neat bonus feature
}
func doSomething() {
// exectued twice, since we call "execute" manually
print("it works!")
}
}
var dummy: Dummy? = Dummy()
// trigger notification
NC.post(Notification.Name(rawValue: "call doSomething"))
// cleanup is automatic
dummy = nil
// this won't trigger anything
NC.post("Doesn't crash!")
IMPORTANT: Kitz repos fully embrace Swift 3.0 and all the changes it brought. You should use v1.0.0
if your still using Swift 2.x.
For manual installation, you can grab the source directly or through git submodules, then simply:
Notificationz.xcodeproj
file as a subproject (make sure Copy resources
is not enabled)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:
class Sample {
private var keyboardObserver: Observer?
private var reachabilityObserver: 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 = nil
Mazyod (@Mazyod)
Notificationz is released under the MIT license. See LICENSE for details.