封装一个Notification

import UIKit

protocol Notifier {
    associatedtype Notification: RawRepresentable
}

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

class Notice: 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 Notice {
    enum Notification: String {

        case kNewsListRefresh

    }
}

你可能感兴趣的:(封装一个Notification)