测试已测试 | ✓ |
Lang语言 | SwiftSwift |
许可 | MIT |
发布最后发布 | 2017年2月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Loïs Di Qual,TakeScoop Ops 维护。
一个围绕 NSNotificationCenter 的优秀的 Swift 包装器。
此库已弃用,将不再维护。
使用 Swift 3,与 NotificationCenter
一起工作更简洁,所以我们建议直接使用 NotificationCenter.default
。
Kugel
比使用 NotificationCenter
多出的唯一功能是使用 notification name => selector
映射一次订阅多个事件,但这可以在您自己的代码中轻松实现,如下所示扩展
extension NotificationCenter {
func addObserver(_ observer: Any, _ namesAndSelectors: [NSNotification.Name: Selector], object: Any? = nil) {
for (name, selector) in namesAndSelectors {
addObserver(observer, selector: selector, name: name, object: object)
}
}
func removeObserver(_ observer: Any, names: [NSNotification.Name], object: Any? = nil) {
for name in names {
removeObserver(observer, name: name, object: object)
}
}
}
pod 'Kugel'
Kugel.publish("NotificationName")
Kugel.publish("NotificationName", object: anObject)
Kugel.publish("NotificationName", userInfo: ["foo": "bar"])
Kugel.publish("NotificationName", object: anObject, userInfo: ["foo": "bar"])
Kugel.publish(NSNotification(name: "NotificationName"))
let token = Kugel.subscribe("NotificationName") { notification in }
Kugel.subscribe(self, name: "NotificationName", selector: "onNotificationReceived:")
Kugel.subscribe(self, name: "NotificationName", selector: "onNotificationReceived:", object: object)
Kugel.subscribe(self, [
"NotificationName1": "onNotification1Received:",
"NotificationName2": "onNotification2Received:",
])
Kugel.subscribe(self, [
"NotificationName1": "onNotification1Received:",
"NotificationName2": "onNotification2Received:",
], object: object)
Kugel.unsubscribe(token)
Kugel.unsubscribe(self, name: "NotificationName")
Kugel.unsubscribe(self, name: "NotificationName", object: object)
Kugel.unsubscribe(self, [
"NotificationName1",
"NotificationName2"
])
Kugel.unsubscribe(self, [
"NotificationName1",
"NotificationName2"
], object: object)
Kugel.unsubscribe(self)
此项目受 MIT 许可证保护。完整许可证可在以下位置找到: https://github.com/TakeScoop/Kugel/blob/master/LICENSE
Bone icon 由 Freepik 制作,来自 http://www.flaticon.com