This is a tiny swift wrapper around NSNotificationCenter with generics, that aids in the creation of Notifications.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding SimplifiedNotificationCenter as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/0x384c0/SimplifiedNotificationCenter.git")
]
SimplifiedNotificationCenter is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SimplifiedNotificationCenter"
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
To run the example project, clone the repo, and run pod install
from the Example directory first.
ios 11.0 and higher
swift v5.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.