TypedNotification
使用通知的方便方式,减少硬编码
安装
pod 'TypedNotification'
或直接将文件复制到您的项目中
示例
- 定义
extension Notification.Name {
static let test = Notification.Name.init("com.typedNotification.test")
}
struct TestNotificaiton: TypedNotification {
var name: Notification.Name { return .test }
var userInfo: [AnyHashable : Any]? {
return ["passedNum": num, "passedStr": str]
}
var num: Int
var str: String
init(_ notification: Notification) {
num = notification.userInfo!["passedNum"] as! Int
str = notification.userInfo!["passedStr"] as! String
}
init(_ num: Int, str: String) {
self.num = num
self.str = str
}
}
- 发布
let testDescriptor = TestNotificaiton.init(1, str: "it is fine")
testDescriptor.post()
- 接收
let token = TestNotificaiton.observer(for: .test) { (testObj) in
print("\(testObj.num) \(testObj.str)")
}
## 许可证
TypedNotification 采用 MIT 许可证。请参阅LICENSE 文件以获取更多信息。