美文网首页
封装一个Notification

封装一个Notification

作者: 彡廿 | 来源:发表于2017-06-16 15:36 被阅读42次
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

      本文链接:https://www.haomeiwen.com/subject/gqkmqxtx.html