SimplifiedNotificationCenter
这是一个围绕 NSNotificationCenter 的封装器,具有泛型,有助于创建通知。
安装
SimplifiedNotificationCenter 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中:
pod "SimplifiedNotificationCenter"
如果使用 swift 2.2,请在您的 Podfile 中添加以下行:
pod "SimplifiedNotificationCenter",
:git => 'https://github.com/0x384c0/SimplifiedNotificationCenter.git',
:branch => 'swift2.2'
用法
将 import SimplifiedNotificationCenter
添加到源代码中
// 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
在不同应用部分传递通知
通知持有者
class Notifications{
let testNotification = SimpleNotification<String> (name: "Example.testNotification")
}
通知处理器
class SampleClass {
//instance of notifications holder
var notifications = Notifications()
init(){
//subscribe
notifications.testNotification.subscribe{ value in
print("value: \(value)")
}
}
}
通知调用者
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
}
}
示例
let
sampleClass = SampleClass(),
anotherClass = AnotherClass()
anotherClass.post()
//comunicationBetweenDifferentClassesExample Test text will be printed
示例应用
要运行示例项目,首先克隆仓库,然后在示例目录中运行 pod install
。
需求
ios 8.0 及更高版本
swift v4.0
单元测试
SimplifiedNotificationCenter 包含 Tests 子目录中的单元测试套件。您可以通过执行平台框架上的测试动作来简单地运行这些测试。
作者
0x384c0, [email protected]
许可
SimplifiedNotificationCenter 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。