我使用Alamofire-beta3,我想自定义响应序列化,但是我收到错误“无法调用非函数类型的值'NSHTTPURLResponse?' “
https://cloud.githubusercontent.com/assets/7556575/10444657/b0d691fc-719b-11e5-878e-bdd46d03be3b.png
我的代码如下:
public static func objectSerializer <T: MTLModel> () -> ResponseSerializer <T, MTLError> {
return ResponseSerializer { request, response, data, error in
// Http Error with Http status code
guard error == nil else {
let failureReason = "Network Error"
let error = MTLError.errorWithCode(error!.code, failureReason: failureReason)
return .Failure(error)
}
// data be null
guard let validData = data where validData.length > 0 else {
let failureReason = "JSON could not be serialized. Response data was nil or zero length."
let error = MTLError.errorWithCode(.ResponseDataNull, failureReason: failureReason)
return .Failure(error)
}
do {
let JSON = try NSJSONSerialization.JSONObjectWithData(validData, options: .AllowFragments)
// response data is not a obejct json
guard let json = JSON as? [String : AnyObject] else {
let failureReason = "JSON could not be serialized. Response data is not a obejct JSON"
let error = MTLError.errorWithCode(.JSONSerializeToObjectFailed, failureReason: failureReason)
return .Failure(error)
}
// Http request successful(state 200 OK), but response data not include `Error Section`
guard let errorDict = json["error"] as? [String : AnyObject] else {
let failureReason = "JSON could not be serialized. Http request successful(state 200 OK), but response data not include `Error Section`"
let error = MTLError.errorWithCode(.JSONSerializeErrorSectionFailed, failureReason: failureReason)
return .Failure(error)
}
// mean request failed
if errorDict.count != 0 {
let error: MTLError!
do {
error = try MTLJSONAdapter.modelOfClass(MTLError.self, fromJSONDictionary: errorDict) as! MTLError
} catch _ {
let failureReason = "MTLError object serialize failed"
error = MTLError.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
}
return .Failure(error)
} else {
// mean request successful
let dataDict = json["data"] as? [String : AnyObject]
do {
let object = try MTLJSONAdapter.modelOfClass(T.self, fromJSONDictionary: dataDict) as! T
return .Success(object)
} catch _ {
let failureReason = "\(T.self) object serialize failed"
let error = MTLError.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
return .Failure(error)
}
}
} catch _ {
let failureReason = "Network Error"
let error = MTLError.errorWithCode(error!.code, failureReason: failureReason)
return .Failure(error)
}
}
}
public func responseObject<T: MTLModel> (queue: dispatch_queue_t? = nil, willStart: (() -> Void)? = nil, didStop: (() -> Void)? = nil, completionHandler: Response<T, NSError> -> Void) -> Self
{
willStart?()
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
return response(responseSerializer: Request.objectSerializer(), completionHandler: { (response: Response<T, NSError>) in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
self.printResponse(response)
dispatch_async(queue ?? dispatch_get_main_queue()) {
didStop?()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
completionHandler(response)
}
})
})
}
我看到https://github.com/Alamofire/Alamofire/issues/817,但我仍然不知道如何更改我的代码。请帮帮我。