This is a tiny swift wrapper around NSNotificationCenter with generics, that aids in the creation of Notifications.
SimplifiedNotificationCenter is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SimplifiedNotificationCenter"
If you using swift 2.2, add the following line to your Podfile:
pod "SimplifiedNotificationCenter",
:git => 'https://github.com/0x384c0/SimplifiedNotificationCenter.git',
:branch => 'swift2.2'
Add import SimplifiedNotificationCenter
to your source code
// create notification
let notification = SimpleNotification<String>(name: "Example.notification")
// subscribe
notification.subscribe {(value, sender) in
print("sender: \(sender.dynamicType)")
print("value: \(value.title)")
}
// post
notification.post("sample text")
//sample text and sender will be printed
Passing notifications between different places of application
Notifications holder:
class Notifications{
let testNotification = SimpleNotification<String> (name: "Example.testNotification")
}
Notifications handler:
class SampleClass {
//instance of notifications holder
var notifications = Notifications()
init(){
//subscribe
notifications.testNotification.subscribe{(value, _) in
print("value: \(value)")
}
}
}
Notifications caller:
class AnotherClass {
func post(){
//post
Notifications().testNotification.post("comunicationBetweenDifferentClassesExample Test text")
//or use
//SimpleNotification<String>(name: "Example.testNotification").post("comunicationBetweenDifferentClassesExample Test text")
//but it is not safe
}
}
Example:
let
sampleClass = SampleClass(),
anotherClass = AnotherClass()
anotherClass.post()
//comunicationBetweenDifferentClassesExample Test text will be printed
To run the example project, clone the repo, and run pod install
from the Example directory first.
ios 8.0 and higher
swift v3.0
SimplifiedNotificationCenter includes a suite of unit tests within the Tests subdirectory. These tests can be run simply be executed the test action on the platform framework you would like to test.
0x384c0, 0x384c0@gmail.com
SimplifiedNotificationCenter is available under the MIT license. See the LICENSE file for more info.