安装PromiseKit(6.2.1)。 Swift4.0。
import UIKit
import PromiseKit
class ViewController: UIViewController {
func onFulfilled() -> (String) -> Promise<String> {
return { result in
return Promise<String> { seal in
print(result)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
seal.fulfill("Hello")
})
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
Promise.value("first")
.then(onFulfilled())
.then(onFulfilled())
.then(onFulfilled())
.then { result in print(result) } // Cannot convert value of type '(Any) -> ()' to expected argument type '(_) -> _'
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
无法将类型'(Any)->()'的值转换为预期的参数类型'(_)-> _'
如何修复.then { result in print(result) }
?
答案 0 :(得分:0)
将then
替换为done
。
_ = Promise.value("first")
.then(onFulfilled())
.then(onFulfilled())
.then(onFulfilled())
.done { result in print(result) }
请参阅:https://github.com/mxcl/PromiseKit/blob/master/Documentation/GettingStarted.md
完成与那时相同,但是您不能返回承诺。它通常是链的“成功”部分的末端。