2017-04-11 45 views
-3
protocol PubSubEvent { 
    associatedtype EventResult 
    static func eventName() -> String 
    func event() -> EventResult 
    func send() 
} 

class BGEventBus: EventBus { 

    static let sharedInstance = BGEventBus() 

    init() { 
     super.init(queue: OperationQueue()) 
    } 

} 


class BGEventBusEvent: PubSubEvent { 

    typealias EventResult = BGEventBusEvent 

    class func eventName() -> String { 
     return String(describing: self) 
    } 

    func send() { 
     BGEventBus.sharedInstance.send(event: self) 
    } 

    func event() -> BGEventBusEvent.EventResult { 
     return self 
    } 
} 


class BGDidLoginEvent: BGEventBusEvent { 
    typealias EventResult = BGDidLoginEvent 

    var password: String? 
    var facebookToken: String? 

    init(password: String? = nil, facebookToken: String? = nil) { 
     self.password = password 
     self.facebookToken = facebookToken 
    } 

} 


class EventBus { 

    var queue: OperationQueue 

    init(queue: OperationQueue) { 
     self.queue = queue 
    } 

    func send(event: AnyObject) { 

    } 

    func handleEvent<T: PubSubEvent>(target:EventBusObservable, handleBlock: ((T.EventResult) -> Void)!) where T.EventResult == T { 

    } 

} 

class EventBusObserver { 

    var objectProtocol: NSObjectProtocol? 

    func addObserver(forName name: NSNotification.Name?, object obj: Any?, queue: OperationQueue?, using block: @escaping (Notification) -> Swift.Void) { 
     self.objectProtocol = NotificationCenter.default.addObserver(forName: name, object: obj, queue: queue, using: block) 
    } 

    deinit { 
     if let obj = self.objectProtocol { 
      NotificationCenter.default.removeObserver(obj) 
     } 
     self.objectProtocol = nil 
     print("deinit observer!") 
    } 
} 

protocol EventBusObservable { 
    func handleBGEvent<T: PubSubEvent>(handleBlock: ((T.EventResult) -> Void)!) where T.EventResult == T 
} 

extension EventBusObservable { 
    func handleBGEvent<T>(handleBlock: ((T) -> Void)!) where T : PubSubEvent, T.EventResult == T { 
     BGEventBus.sharedInstance.handleEvent(target: self, handleBlock:handleBlock) 
    } 
} 

class sample: EventBusObservable { 
    func test() { 
     self.handleBGEvent { (event: BGDidLoginEvent) in 

     } 
    } 
} 

好家伙我更新的Xcode 8.3,现在我得到了一些象这样的错误:的Xcode 8.3误差与仿制药

无法将类型的价值“(BGDidLoginEvent) - )>(” ((_) - > Void)!'''

任何人都可以帮我吗?

这里的示例文件https://drive.google.com/open?id=0B1zPtsTG7crPQncxYnEyWTBpSXM

+0

'''类BGEventBus:EventBus { 静态让sharedInstance = BGEventBus() 的init(){ super.init(队列:OperationQueue() ) } } ''' –

+0

我刚刚更新了示例代码 –

+0

马特嗨比较遗憾的是,这里的所有代码的文件复制,我有错误https://drive.google.com/open?id = 0B1zPtsTG7crPQncxYnEyWTBpSXM,谢谢。 –

回答

0

我认为你必须写通用要求正是每次都以同样的方式。所以,在EventBus:

class EventBus { 
    // ... 
    func handleEvent<T>(target:EventBusObservable, handleBlock: ((T) -> Void)!) where T : PubSubEvent, T.EventResult == T { 
    } 
} 

在EventBusObservable:

protocol EventBusObservable { 
    func handleBGEvent<T>(handleBlock: ((T) -> Void)!) where T : PubSubEvent, T.EventResult == T 
} 

在EventBusObservable扩展:

extension EventBusObservable { 
    func handleBGEvent<T>(handleBlock: ((T) -> Void)!) where T : PubSubEvent, T.EventResult == T { 
     BGEventBus.sharedInstance.handleEvent(target: self, handleBlock: handleBlock) 
    } 
} 

,编译。最后,我们留给你的班级sample。这个并不容易,我发现我不得不宣布event作为BGEventBusEvent:

class sample: EventBusObservable { 
    func test() { 
     self.handleBGEvent { 
      (event:BGEventBusEvent) in 
     } 
    } 
}