TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | May 2015 |
SPMSupports SPM | ✗ |
Maintained by Max Campolo.
MCNotificationCenter
is a replacement for calls to NSNotificationCenter that makes dealing with notifications easier. The singleton class manages and tracks registered observers so you don’t have to! Use MCNotificationCenter
to prevent observer-related crashes when registering observers and sending notifications.
Using Cocoapods:
$ pod 'MCNotificationCenter'
Or copy MCNotificationCenter.swift
and NSObject+EasyKVO.swift
into a project. Add to a bridging header if necessary.
Check out the example project in the repo for full code samples.
All calls should use the MCNotificationCenter.defaultCenter
singleton
MCNotificationCenter.defaultCenter
Add an observer using one of the provided methods. It follows a similar structure to NSNotificationCenter.
MCNotificationCenter.defaultCenter.addObserver(self, selector: Selector("printNotification:"), name: "myNotification", object: nil)}
Remove an observer. Checking if the observer exists is optional, it’s safe to call remove even if the observer doesn’t exist.
if MCNotificationCenter.defaultCenter.isObserverForName("myNotification", observer: self) {
MCNotificationCenter.defaultCenter.removeObserverWithName("myNotification", observer: self)
}
Note: You can remove all registered observers for a class or object.
Add a key value observer with a closure (setting MCNotificationCenter.defaultCenter is required for this functionality. To implement a different observer class, set the class as the observer and nil for the block)
MCNotificationCenter.defaultCenter.addKeyValueObserver(MCNotificationCenter.defaultCenter, onObject: self.myObject, forKeyPath: "someValue", options: .New, context: nil) { () -> () in
println(self.myObject.someValue)
}
Or use the NSObject+EasyKVO
extension which provides convenience methods for using MCNotificationCenter functions
myObject.addObserver("someValue", usingBlock: { () -> () in
println(self.myObject.value)
})
And remove easily using just the keypath
myObject.removeObserver("someValue")
Check out the files and code comments.
Questions, comments or ideas? Feel free to send me a message or post an issue.
For fixes, additions, improvements or any other cool stuff pull requests welcome.