Notifwift

Version License Platform Carthage Compatible

Notifwift 是 NSNotificationCenter 的 Swift 封装。

Notifwift 解决了以下问题;

用法

    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

接收器闭包接受

    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"

作者

takasek

许可协议

Notifwift 在 MIT 许可协议下可用。 有关更多信息,请参阅 LICENSE 文件。