PromiseKit 6 错误 in cannot convert 错误

PromiseKit 6 error in cannot convert error

首先,我知道 v6 中的实现发生了变化,并且我按预期使用了 seal 对象,我遇到的问题是,即使严格按照示例进行操作,它仍然给我旧的 Cannot convert value of type '(_) -> CustomerLoginResponse' to expected argument type '(_) -> _' 错误。

这是我的函数,returns 承诺:

 static func makeCustomerLoginRequest(userName: String, password: String) -> Promise<CustomerLoginResponse>
{
    return Promise
        { seal in
            Alamofire.request(ApiProvider.buildUrl(), method: .post, parameters: ApiObjectFactory.Requests.createCustomerLoginRequest(userName: userName, password: password).toXML(), encoding: XMLEncoding.default, headers: Constants.Header)
                     .responseXMLObject { (resp: DataResponse<CustomerLoginResponse>) in
                if let error =  resp.error
                {
                    seal.reject(error)
                }
                guard let Xml = resp.result.value else {
                    return seal.reject(ApiError.credentialError)
                }
                seal.fulfill(Xml)
            }
    }
}

这是使用它的函数:

static func Login(userName: String, password: String) {
    ApiClient.makeCustomerLoginRequest(userName: userName, password: password).then { data -> CustomerLoginResponse  in

    }
}

如果您想链接多个 promises,您可能需要提供更多信息。在 v6 中,如果您不想继续 promises 链,则需要使用 .done。如果您只有一个 promise 这个请求,那么下面是正确的实现。

static func Login(userName: String, password: String) {
    ApiClient.makeCustomerLoginRequest(userName: userName, password: password)
         .done { loginResponse in
              print(loginResponse)
         }.catch { error in
              print(error)
         }
}

请记住,如果您使用 .then,则必须 return 一个 promise,直到您使用 .done 打破链条。如果你想链接多个 promises 那么你的语法应该是这样的,

ApiClient.makeCustomerLoginRequest(userName: userName, password: password)
       .then { loginResponse -> Promise<CustomerLoginResponse> in
             return .value(loginResponse)
        }.then { loginResponse -> Promise<Bool> in
             print(loginResponse)
             return .value(true)
        }.then { bool -> Promise<String> in
             print(bool)
             return .value("hello world")
        }.then { string -> Promise<Int> in
             print(string)
             return .value(100)
        }.done { int in
             print(int)
        }.catch { error in
             print(error)
        }