Swiftswift学习iOS-swift

Swift - 优雅的实现通知-发送、接收、移除

2016-10-31  本文已影响2411人  小黑Swift

Swift优雅的使用通知NSNotification

SnailNotice.swift
   import UIKit

    protocol Notifier {
        associatedtype Notification: RawRepresentable
        
    }

    extension Notifier where Notification.RawValue == String {
        
       static func nameFor(notification: Notification) -> String {

            return "\(notification.rawValue)"
        }
    }

    class SnailNotice: Notifier {
        
        /// 发送通知
        static func post(notification: Notification, object:AnyObject? = nil) {
            
            let name = nameFor(notification: notification)
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: name), object: object)
        }
        
        /// 增加观察 - 接收通知
        static func add(observer: AnyObject, selector: Selector, notification: Notification, object:AnyObject? = nil) {
            
            let name = nameFor(notification: notification)
            NotificationCenter.default
                .addObserver(observer, selector: selector, name: NSNotification.Name(rawValue: name), object: object)
        }
        
        /// 移除观察 - 移除通知
        static func remove(observer: AnyObject, notification: Notification, object:AnyObject? = nil) {
            
            let name = nameFor(notification: notification)
            NotificationCenter.default.removeObserver(observer, name: NSNotification.Name(rawValue: name), object: object)
        }
    }

    // 定义的通知名字
    extension SnailNotice {
        enum Notification: String {
            /// 开心
            case happy
            /// 伤心
            case sad
            /// 睡觉
            case sleep
            /// ....
            case 🍎
        }
    }
  // swift 3.0

使用例子:

import UIKit

    class ViewController: UIViewController {
        
        //增加通知
        var myObserver:Bool? {
            didSet {
                _ = SnailNotice.add(observer: self, selector: #selector(reload), notification: .happy)
                _ = SnailNotice.add(observer: self, selector: #selector(reload), notification: .sad)
                _ = SnailNotice.add(observer: self, selector: #selector(reload), notification: .sleep)
            }
        }
        
        //移除通知
        deinit {
            SnailNotice.remove(observer: self, notification: .happy)
            SnailNotice.remove(observer: self, notification: .sad)
            SnailNotice.remove(observer: self, notification: .sad)
        }

        override func viewDidLoad() {
            super.viewDidLoad()
            
            myObserver = true //开启所有观察
            
            //发送通知
            SnailNotice.post(notification: .happy)
        }
        
        
        func reload() {
            print("重载数据")
        }
        
        
    //    deinit {
    //        let allNotice:[SnailNotice.Notification] = [.happy,.sad,.sleep,.🍎]
    //        allNotice.forEach {
    //            SnailNotice.remove(observer: self, notification: $0)
    //        }
    //    }
    }
上一篇下一篇

猜你喜欢

热点阅读