从 PromiseKit wrap 迁移
Migrating from PromiseKit wrap
我正试图摆脱代码中的一些警告,但似乎无法从 PromiseKit 的 wrap
中迁移出来。具体来说,在 6.0 中,发布详细信息说我不应该使用它,而应该使用 init(resolver:)
。
我有一个功能:
func foo(arg1: Int, arg2: Int, completionHandler: @escaping () -> ())
以下是有效的:
wrap({ foo(arg1: val1, arg2: val2, completionHandler: [=11=]) })
我尝试将其更改为(发行说明建议的内容):
Promise { foo(arg1: val1, arg2: val2, completionHandler: [=12=].resolve) }
这产生了一个错误 Generic parameter 'T' could not be inferred
所以我试图解决这个问题:
Promise<Void> { foo(arg1: val1, arg2: val2, completionHandler: [=13=].resolve) }
但这引发了不同的错误 Unable to infer closure type in the current context
,我不确定从那里去哪里。
下面是 deprecated
用于 wrap
方法的方法。
@available(*, deprecated, message: "See `init(resolver:)`")
public func wrap(_ body: (@escaping (Error?) -> Void) throws -> Void) -> Promise<Void> {
return Promise { seal in
try body(seal.resolve)
}
}
正如我们所见,completion
闭包采用了 Optional
Error
参数,因此我怀疑您的旧代码是如何通过传递错误的闭包来工作的。我觉得你的foo
方法声明应该是这样的,
func foo(arg1: Int, arg2: Int, completionHandler: @escaping (Error?) -> Void) {
// body
}
然而,对于最新的 PromiseKit
,您可以通过传递 Optional
Error
来更新 completionHandler
,如下所示创建 Promises
,
Promise { foo(arg1: 1, arg2: 2, completionHandler: [=12=].resolve) }
我正试图摆脱代码中的一些警告,但似乎无法从 PromiseKit 的 wrap
中迁移出来。具体来说,在 6.0 中,发布详细信息说我不应该使用它,而应该使用 init(resolver:)
。
我有一个功能:
func foo(arg1: Int, arg2: Int, completionHandler: @escaping () -> ())
以下是有效的:
wrap({ foo(arg1: val1, arg2: val2, completionHandler: [=11=]) })
我尝试将其更改为(发行说明建议的内容):
Promise { foo(arg1: val1, arg2: val2, completionHandler: [=12=].resolve) }
这产生了一个错误 Generic parameter 'T' could not be inferred
所以我试图解决这个问题:
Promise<Void> { foo(arg1: val1, arg2: val2, completionHandler: [=13=].resolve) }
但这引发了不同的错误 Unable to infer closure type in the current context
,我不确定从那里去哪里。
下面是 deprecated
用于 wrap
方法的方法。
@available(*, deprecated, message: "See `init(resolver:)`") public func wrap(_ body: (@escaping (Error?) -> Void) throws -> Void) -> Promise<Void> { return Promise { seal in try body(seal.resolve) } }
正如我们所见,completion
闭包采用了 Optional
Error
参数,因此我怀疑您的旧代码是如何通过传递错误的闭包来工作的。我觉得你的foo
方法声明应该是这样的,
func foo(arg1: Int, arg2: Int, completionHandler: @escaping (Error?) -> Void) {
// body
}
然而,对于最新的 PromiseKit
,您可以通过传递 Optional
Error
来更新 completionHandler
,如下所示创建 Promises
,
Promise { foo(arg1: 1, arg2: 2, completionHandler: [=12=].resolve) }