Notifwift
Notifwift 是 NSNotificationCenter 的 Swift 包装器。
Notifwift 解决了:
- 管理 Notification 观察者累赘的问题
- 对于 Notification.userInfo 要求的无意义的字典键
- 发布/观察通知时长语法的问题
使用方法
let notificationName = Notification.Name(rawValue: "YourNotificationName")
do {
let notifwift = Notifwift()
notifwift.observe(notificationName) {(notification) in
print("Notifwift can observe Notification in simple way.", notification)
}
Notifwift.post(notificationName)
//printed:
// Notifwift can observe Notification in simple way. name = YourNotificationName, object = nil, userInfo = nil
}
Notifwift.post(notificationName)
//printed nothing. Observers expire when the Notifwift instance(in this case) is destructed.
let notifwift = Notifwift()
notifwift.observe(notificationName) { (payload: String) in
print("This closure observes nothing but Notification with String payload.", payload)
}
notifwift.observe(notificationName) { (payload: Int) in
print("This closure observes nothing but Notification with Int payload.", payload)
}
Notifwift.post(notificationName, payload:"aaaa")
//printed:
// This closure observes nothing but Notification with String payload. aaaa
Notifwift.post(notificationName, payload:1)
//printed:
// This closure observes nothing but Notification with Int payload. 1
接收器块接受
(Notification) -> Void
(Notification, T) -> Void
(T) -> Void
class Animal {}
class Cat: Animal {}
let notifwift = Notifwift()
notifwift.observe(notificationName) { (_, p:Animal) in
print("Received Animal.", p)
}
notifwift.observe(notificationName) { (_, p:Cat) in
print("Received Cat. Yes, of course, Notifwift recognizes subtypes.", p)
}
Notifwift.post(notificationName, payload:Animal())
//printed:
// Received Animal. (Animal #1)
Notifwift.post(notificationName, payload:Cat())
//printed:
// Received Animal. (Cat #1)
// Received Cat. Yes, of course, Notifwift recognizes subtypes. (Cat #1)
enum SomeResult {
case Success(String)
case Fail(NSError)
}
let notifwift = Notifwift()
notifwift.observe(notificationName) { (_, p:SomeResult) in
switch p {
case .Success(let str):
print("Any Type can be used as a payload", str)
case .Fail(let err) where err.code == 403:
print("not authorized")
case .Fail(let err) where err.code == 404:
print("not found")
case .Fail(let err):
print("Notifwift has a chemistry with Enum Associated Values.", err)
}
}
Notifwift.post(notificationName, payload:SomeResult.Success("like this."))
//printed:
// Any Type can be used as a payload like this.
Notifwift.post(notificationName, payload:SomeResult.Fail(NSError(domain: "", code: 0, userInfo: nil)))
//printed:
// Notifwift has a chemistry with Enum Associated Values. Error Domain= Code=0 "(null)"
let obj1 = NSObject()
let obj2 = NSObject()
let notifwift = Notifwift()
notifwift.observe(notificationName) { _ in
print("Received from all objects")
}
notifwift.observe(notificationName, from: obj1) { _ in
print("Received from obj1 only")
}
notifwift.observe(notificationName, from: obj2) { _ in
print("Received from obj2 only")
}
Notifwift.post(notificationName, from: obj1)
//printed:
// Received from all objects
// Received from obj1 only
Notifwift.post(notificationName, from: obj2)
//printed:
// Received from all objects
// Received from obj2 only
由于在 Notifwift 实例死亡时,注册的观察者将自动销毁,因此您可以自己销毁它们。
notifwift.dispose(dispose)
实际案例
这是一个使用示例。
let didReceiveUserNotification = Notification.Name(rawValue: "didReceiveUserNotification")
final class MyViewController: UIViewController {
@IBOutlet var userInfoView: MyUserInfoView!
let notifwift = Notifwift()
override func viewDidLoad() {
super.viewDidLoad()
notifwift.observe(didReceiveUserNotification) { [unowned self] in self.reload($0) } //Use `weak` or `unowned` on calling the self methods to avoid retain cycles.
}
private func reload(user: User) {
userInfoView.reload(user)
}
}
final class MyUserRepository {
func fetchUser(id: Int) {
MyAPIManager.fetchUser(id) { (user: User) in
Notifwift.post(didReceiveUserNotification, payload: user)
}
}
}
Notifwift 实例在 MyViewController 实例存活期间存在。它在 MyViewController 实例死亡时死亡,因此注册在 Notifwift 实例中的观察者将被自动删除。如果需要管理观察者,则无需关心!
安装
Notifwift 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中
pod "Notifwift"
Notifwift 可通过 Carthage 获取。要安装它,只需将以下行添加到您的 Cartfile 中
github "takasek/Notifwift"
作者
许可
Notifwift 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。