SimplifiedNotificationCenter
This is a tiny swift wrapper around NSNotificationCenter with generics, that aids in the creation of Notifications.
Installation
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'
Usage
Add import SimplifiedNotificationCenter
to your source code
// create notification
let notification = SimpleNotification<String>(name: "Example.notification")
// subscribe
notification.subscribe { value in
print("value: \(value)")
}
//scheck is notification subscribed
print(notification.isSubscribed)
//post
notification.post("sample text")
//sample text 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
Example App
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requirements
ios 8.0 and higher
swift v4.0
Unit Tests
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.
Author
0x384c0, [email protected]
License
SimplifiedNotificationCenter is available under the MIT license. See the LICENSE file for more info.