Swift自定义事件分发

Swift自带有Notification,可以作为事件分发用,下面是自定义的一种事件分发,使用观察者模式

/// 自定义消息分发类,为单例
/// 注意:添加侦听后,一定要移除
public final class Messager{
    
    private static var _shared:Messager?
    
    public static var shared:Messager!{
        get{
            if _shared == nil {
                _shared = Messager()
                return _shared!
            }
            return _shared!
        }
    }
    
    private init(){}
    
    
    /// key为事件名,value为元组(target: 对象, call: 方法)
    private var evts = DictionaryVoid)]>()
    
    
    /// 添加侦听
    ///
    /// - Parameters:
    ///   - event: 事件名
    ///   - target: 对象
    ///   - callBack: 回调方法
    public func addListener(_ event:String!,target:AnyObject?,callBack:@escaping (Any)->Void )
    {
        var list = evts[event]
        if list == nil {
            list = []
        }
        list?.append((target:target,call:callBack))
        evts[event] = list
    }
    
    
    /// 通过事件名删除侦听
    ///
    /// - Parameter event: 事件名
    public func removeListener(_ event:String!)->Void
    {
        if evts[event] != nil
        {
            evts.removeValue(forKey: event)
        }
    }
    
    
    /// 通过事件名和对象移除侦听
    ///
    /// - Parameters:
    ///   - event: 事件名
    ///   - target: 对象
    public func removeListener(_ event:String! , target: AnyObject? )
    {
        var list = evts[event]
        if list != nil {
            for (i,element) in list!.enumerated() {
                if target === element.target {
                    list?.remove(at: i)
                    if list?.count == 0 {
                        self.evts.removeValue(forKey: event)
                    }
                    break
                }
            }
        }
    }
    
    
    /// 抛出事件
    ///
    /// - Parameters:
    ///   - event: 事件名
    ///   - data: 数据
    public func dispatch(_ event:String!, data:Any?)
    {
        let list = evts[event]
        if list != nil
        {
            for element in list! {
                element.call(data as Any)
            }
        }
    }
    
    
    /// 清除所有侦听
    public func clearAll()->Void{
        evts.removeAll()
    }
}

//测试
Messager.shared.addListener("EVENT_OK",target:nil,callBack: { (data) in
    print( data as! Int )
})
//Messager.shared.addListener("EVENT_ERROR",target:nil,callBack: { (data) in
//    print("Error: \(data as! Int)" )
//})

Messager.shared.dispatch("EVENT_OK", data: 3)
Messager.shared.removeListener("EVENT_OK",target:nil)
Messager.shared.dispatch("EVENT_OK", data: 3)
Messager.shared.dispatch("EVENT_ERROR", data:5)

可惜没找到如何判断两个方法相等,不然可以通过事件名和方法来移除侦听




你可能感兴趣的:(swift,swift)