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)
    //        }
    //    }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 万水千山就当是伏笔 只为遇到姗姗来迟的你
    如果我的米修会说话阅读 152评论 0 0
  • 夜 黑暗的河 轰轰隆隆 碾过 空 目送 一别 三年 清 仰头 无人等 家 无灯 心 迷茫 无所踪 何处去 释愁容 ...
    昂格伦阅读 400评论 2 3
  • 01 先讲一个真实的故事: 儿子7岁,小学二年级。一天,儿子回家对我说:“爸爸,能不能给我零花钱?” “嗯?”我一...
    三少爷的自修之路阅读 466评论 2 8
  • 午间在食堂就餐,两个同事八卦一个跟我交情不错的姐姐,这个姐姐现在正在修产假阶段。我的座位跟她俩中间隔着另一个同事,...
    徐行者也阅读 148评论 0 0