我使用alamofire。在将代码转换为swift3之前,一切正常,但在调用我的方法转换为swift3后...数据为空。
getRequest返回NSMutableURLRequest:
private func getRequest(_ soapAction: String, urlService: URL, methodName: String, params: NSArray?) -> NSMutableURLRequest {
var newSoapMessage = ""
if let _ = params {
newSoapMessage = getSoapMessage(methodName, params: params!)
} else {
newSoapMessage = getSoapMessage(methodName)
}
request = NSMutableURLRequest(url: urlService)
lengthSoapMessage = String(newSoapMessage.characters.count)
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(lengthSoapMessage, forHTTPHeaderField: "Content-Length")
request.addValue("Basic \(accountBase64)", forHTTPHeaderField: "Authorization")
request.addValue(soapAction, forHTTPHeaderField: "SoapAction")
request.httpMethod = "POST"
request.httpBody = newSoapMessage.data(using: String.Encoding.utf8)
return request
}
现在:
var request = getRequest(soapAction,urlService: Common.urlShatelService! as URL, methodName: methodName, params: params)
Alamofire.request(request as! URLRequestConvertible).responseData { response in
switch response.result {
case .success(let value):
let result = Result.success(value)
callBack(result)
break
case .failure:
let result = Result<Data>.failure
callBack(result)
print("exception: \(response.result.error)")
break
}
}
转换之前,alamofire接受请求但转换后我必须转换为URLRequestConvertible !!!!
现在。当调用alamofire返回此异常时:
2016-09-27 17:17:57.267619[3454:1060477] Could not cast value
of type 'NSMutableURLRequest' (0x101e445b8) to
'Alamofire.URLRequestConvertible' (0x101e400b0).
答案 0 :(得分:5)
我有同样的问题。 尝试更改URLRequest的NSMutableURLRequest类型。
request = URLRequest(url: urlService)