xcode 8.3泛型错误

时间:2017-04-11 22:32:43

标签: ios swift xcode generics

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,现在我遇到了这样的错误:

  

无法转换类型&#39;(BGDidLoginEvent)的值 - &gt; ()&#39;预期参数类型&#39;((_) - &gt; Void)!&#39;&#39;

任何人都可以帮助我吗?

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

1 个答案:

答案 0 :(得分: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
        }
    }
}