无法使用Promise调用Alamofire请求

时间:2018-12-14 08:39:37

标签: swift alamofire promisekit

我建立了一个简单的客户端来联系我的api-这是有效的功能之一:

static func makeCustomerLoginRequest(userName: String, password: String) -> Promise<CustomerLoginResponse>
{
    let requestObject = CustomerLoginRequest(CompanyToken: "XXXX", customerName: userName, customerPass: password)
    return Promise
        { seal in
            Alamofire.request(ApiProvider.buildUrl(), method: .post, parameters: requestObject.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)
            }
    }
}

我注意到采用这种方法的第一件事是重复很多代码,我想消除这种情况,但是尝试移出一些通用代码似乎使Alamofire感到沮丧。这是我的重构方法: 首先,通用方法(位于ApiProvider类中)

static func buildDataRequest(requestObject: XMLMappable) -> Promise<DataRequest>
{
    return Promise
    { seal in
        seal.fulfill(Alamofire.request(buildUrl(), method: .post, parameters: requestObject.toXML(), encoding: XMLEncoding.default, headers: Constants.Header))
    }
}

接下来是新的makeCustomerLoginRequest方法:

static func makeCustomerLoginRequest(userName: String, password: String) -> CustomerLoginResponse
{
    let requestObject = CustomerLoginRequest(CompanyToken: “XXXXX”, customerName: userName, customerPass: password)
    ApiProvider.buildDataRequest(requestObject: requestObject).then {
        $0.responseXMLObject(completionHandler: { (resp:DataResponse<CustomerLoginResponse>) in

        })
    }
}

这样做,但是出现以下错误: Cannot invoke 'responseXMLObject' with an argument list of type '(completionHandler: (DataResponse<CustomerLoginResponse>) -> ())'

0 个答案:

没有答案