【问题标题】:xcode 8.3 error with genericsxcode 8.3 泛型错误
【发布时间】: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 中编译。

标签: ios swift xcode generics


【解决方案1】:

我认为您每次都必须完全以相同的方式编写通用要求。所以,在 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
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2010-09-11
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多