首页 > 解决方案 > 从 '(_) throws -> ()' 类型的抛出函数到非抛出函数类型 '(CallResult) -> Void' 的无效转换

问题描述

有以下代码块:

do {
  try call.receiveMessage { callResult in
    if let msg = try callResult.resultData {
      let result = try decoder.decode(valueType, from: msg)
    }
    // other stuff
} catch let error {
  if let callback = self.onError {
    callback(error)
  }
}

由于以下错误而无法编译try call.receieveMessage

从 '(_) throws -> ()' 类型的抛出函数到非抛出函数类型 '(CallResult) -> Void' 的无效转换

我正在调用的函数确实有签名更改:

来自:public func receiveMessage(completion: @escaping (Data?) throws -> Void) throws { to: public func receiveMessage(completion: @escaping (CallResult) -> Void) throws {

我难以理解的是,相同方法的其他调用没有问题,所以我还没有完全确定是什么让这个特定的块特别。

任何意见,将不胜感激。

标签: swift

解决方案


应该坐久一点。解决方案是用 do/catch 包装 lambda。


推荐阅读