我将我的项目从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)
}`
答案 0 :(得分:1)
您应该将回调的NSError?
更改为Error?
,并将失败回复错误转发给Error
。
typealias APIClientResponseCallBack = (_ statusCode: Int?, _ response : AnyObject?, _ error : Error?) -> ()
failureCallBack(client.responseErrorMessage(statusCode: statusCode, response: response, error: error as Error))