欢迎加入QQ讨论群258996829

iOS通知中心封装库SwiftNotificationCenter

发布时间:2017-05-21 23:23  回复:0  查看:6595  感兴趣:70  赞:2   最后回复:2017-05-21 23:23  
SwiftNotificationCenter是一个面向协议的iOS通知中心库,用Swift编写。
特性类型安全、线程安全、内存安全。
  • 类型安全
    没有更多的userInfo字典和向下转型,只要传递具体的值给观察者。
  • 线程安全
    你可以在任何线程中registernotifyunregister,而不会崩溃和数据损坏。
  • 内存安全
    SwiftNotificationCenter将观察者存储为zeroing-weak引用。 没有崩溃,无需手动注销。
它简单、安全、轻便、易于使用,用于一对多通信。
示例代码:
//Define protocol and observer:
protocol Update {
    func updateTitle(title: String)
}

extension ViewController: Update {
  func updateTitle(title: String) {
  		self.titleLabel.text = title
  }
}
let vc = ViewController()


//Register:
Broadcaster.register(Update.self, observer: vc)


//Broadcast:
Broadcaster.notify(Update.self) {
    $0.updateTitle("new title")
}


//Unregister:
Broadcaster.unregister(Update.self, observer: self)


相关开源代码

您还未登录,请先登录

热门帖子

最新帖子