【发布时间】:2017-04-11 22:32:43
【问题描述】:
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,现在我遇到了一些类似这样的错误:
无法将类型 '(BGDidLoginEvent) -> ()' 的值转换为预期的参数类型 '((_) -> Void)!''
谁能帮帮我?
这里是示例文件https://drive.google.com/open?id=0B1zPtsTG7crPQncxYnEyWTBpSXM
【问题讨论】:
-
class BGEventBus: EventBus { static let sharedInstance = BGEventBus() init() { super.init(queue: OperationQueue()) } } -
我刚刚更新了示例代码
-
嗨,Matt 对此感到抱歉,这里有一个包含所有代码的文件,用于复制我遇到的错误 drive.google.com/open?id=0B1zPtsTG7crPQncxYnEyWTBpSXM,谢谢。
-
我会将其粘贴到您的问题中。我不明白为什么你不能粘贴它。
-
好的,我会在 bugs.swift.org 提交一份错误报告,并说明它可以在 Xcode 8.2 中编译,但不是在 Xcode 8.3 中编译。