【问题标题】:PromiseKit 6 error in cannot convert errorPromiseKit 6 错误无法转换错误
【发布时间】:2018-12-14 13:58:07
【问题描述】:

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

这是我返回承诺的函数:

 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

    }
}

【问题讨论】:

    标签: swift alamofire promisekit


    【解决方案1】:

    如果您想链接多个promises,您可能需要提供更多信息。在v6 中,如果您不想继续promise 链,则需要使用.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,则必须返回 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)
            }
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 2013-11-15
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 2017-02-09
      相关资源
      最近更新 更多