【问题标题】:Cannot convert value of type '(Any) -> ()' to expected argument type '(_) -> _'无法将类型 '(Any) -> ()' 的值转换为预期的参数类型 '(_) -> _'
【发布时间】:2018-09-08 13:22:43
【问题描述】:

安装 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) }?

【问题讨论】:

    标签: swift4 promisekit


    【解决方案1】:

    将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

    done 和 then 一样,但是你不能返回一个 Promise。它通常是链中“成功”部分的结束。

    【讨论】:

      猜你喜欢
      • 2021-06-18
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      • 2020-02-04
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多