Alamofire - 从Swift 2转换为swift 3的错误

时间:2017-07-19 07:39:54

标签: ios swift alamofire nserror

我将我的项目从Swift 2.3转换为Swift 3.0。我们正在使用Alamofire pod进行联网。现在我遇到NSError的问题。

当我们写error: responseObj.result.error时,Swift 3不接受这个。帮我解决这个问题。

检查我的代码:

static func getDataFromAPI(parameters : [String : AnyObject], successCallBack : @escaping APISuccessCallBack, failureCallBack : @escaping APIFailureCallBack) {

        client.makePOSTRequest(service: Constants.API_BASE_URL, parameters: parameters) { (statusCode, response, error) -> () in
            if statusCode == MobomoAPIClient.HTTPStatusCode.Success {
                successCallBack(response)
            }
            else {

                failureCallBack(client.responseErrorMessage(statusCode: statusCode, response: response, error: error))
            }
        }
    }


typealias APIClientResponseCallBack = (_ statusCode: Int?, _ response : AnyObject?, _ error : NSError?) -> ()

`self.alamoFireManager!.request(urlString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
            .responseJSON { (responseObj) -> Void in
                responseCallBack(statusCode: responseObj.response?.statusCode, response: responseObj.result.value as AnyObject, error: responseObj.result.error)
        }`

1 个答案:

答案 0 :(得分:1)

您应该将回调的NSError?更改为Error?,并将失败回复错误转发给Error

typealias APIClientResponseCallBack = (_ statusCode: Int?, _ response : AnyObject?, _ error : Error?) -> ()
failureCallBack(client.responseErrorMessage(statusCode: statusCode, response: response, error: error as Error))