我刚开始从swift 2.2-> 3.0转换我的旧项目。在这个过程中,我不得不将Alamofire更新到4.0版以获得对新swift版本的支持。我已经解决了最多的问题,但是这个我很快就会出现?
任何人都可以解释为什么最后一个return语句显示以下错误:
无法调用非函数类型'HTTPURLResponse?'
的值
具体做法是:
返回响应(responseSerializer:responseSerializer,completionHandler:completionHandler)
extension Alamofire.DataRequest {
func responseTVArray(_ completionHandler: @escaping (DataResponse<TVWrapper>, Error?) -> Void) -> Self {
let responseSerializer = DataResponseSerializer<TVWrapper> { request, response, data, error in
guard error == nil else { return .failure(error!) }
guard let responseData = data else
{
let failureReason = "Array could not be serialized because input data was nil."
let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
let error = NSError(domain: "UMAT", code: ErrorCode.DataSerializationFailed.rawValue, userInfo: userInfo)
return .failure(error)
}
let JSONResponseSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)
switch result {
case .success(let value):
let json = SwiftyJSON3.JSON(value)
let wrapper = TVWrapper()
wrapper.page = json["page"].intValue
wrapper.totalPages = json["total_pages"].intValue
wrapper.totalResults = json["total_results"].intValue
var allTV:Array = Array<TV>()
let results = json["results"]
for jsonTV in results
{
let tv = TV(json: jsonTV.1, id: Int(jsonTV.0) )
if (tv.posterPath == "")
{
continue
}
else
{
allTV.append(tv)
}
}
wrapper.results = allTV
return .success(wrapper)
case .failure(let error):
return .failure(error)
}
}
return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}
答案 0 :(得分:3)
@Mat0感谢您的评论。我正在写这个答案,因为在我的案例中我还有更多的问题需要解决。
以下是Swift 2.2
中的方法。
func responseSLJSON(completionHandler: Response<AnyObject, NSError> -> Void,
errorHandler: (String, Result<AnyObject, NSError>) -> NSError = Request.slError()) -> Self {
let responseSerializer = ResponseSerializer<AnyObject, NSError> {
request, response, data, error in
let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
guard error == nil else {
let errorResult = JSONSerializer.serializeResponse(request, response, data, nil)
return .Failure(errorHandler(#function, errorResult))
}
return JSONSerializer.serializeResponse(request, response, data, error)
}
return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}
我把它转换成......
func responseSLJSON(completionHandler: @escaping (DataResponse<Any>) -> Void,
errorHandler: @escaping (String, Result<Any>) -> NSError = DataRequest.slError()) -> Self {
let responseSerializer = DataResponseSerializer<Any> {
request, response, data, error in
let jsonSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
guard error == nil else {
let errorResult = jsonSerializer.serializeResponse(request, response, data, nil)
return .failure(errorHandler(#function, errorResult))
}
return jsonSerializer.serializeResponse(request, response, data, error)
}
return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}
在这种情况下,return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
与参数类型有关。我必须使用Any
而不是AnyObject
,因为jsonResponseSerializer
会返回DataResponseSerializer<Any>
。
public static func jsonResponseSerializer(
options: JSONSerialization.ReadingOptions = .allowFragments)
-> DataResponseSerializer<Any>
同样@escaping
对completionHandler
参数至关重要。