gpt4 book ai didi

swift - 恢复 block 的无效转换,PromiseKit

转载 作者:行者123 更新时间:2023-11-28 14:34:06 25 4
gpt4 key购买 nike

这段代码

let promise: Promise<Supplier> = self.supplierController
.update(supplier: supplier)

let block: ((Error) throws -> Supplier) = { (error: Error) throws -> Supplier in
let supplier: Supplier = supplier
guard (error as NSError).code == 405 else {
throw error
}
return supplier
}

let newPromise =
promise
.recover(block)
.done { (_: Supplier) in
changeCompanyIdAndAppendMessage()
}

给出编译时错误

invalid conversion from throwing function of type '(Error) throws -> Supplier' to non-throwing function type '(Error) -> Guarantee'

为什么要尝试转换?在我看来,它必须使用这个函数:

public func recover(on: DispatchQueue? = default, policy: PromiseKit.CatchPolicy = default, _ body: @escaping (Error) throws -> U) -> PromiseKit.Promise where U : Thenable, Self.T == U.T

来自 PromiseKit

我添加了显式类型并将 promise 分成 block ,以免遗漏任何内容

最佳答案

您应该返回 Promise 以正确链接,如下所示:

self.supplierController
.update(supplier: supplier)
.recover { error -> Promise<Supplier> in
let supplier: Supplier = supplier
guard (error as NSError).code == 405 else {
throw error
}

return .value(supplier)
}
.done { (_: Supplier) in
changeCompanyIdAndAppendMessage()
}

来源:https://github.com/mxcl/PromiseKit/blob/master/Documentation/CommonPatterns.md

关于swift - 恢复 block 的无效转换,PromiseKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51023774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com