无法专门化非泛型类型的ResponseSerializer

时间:2015-09-28 11:07:47

标签: swift2 alamofire

documentation2.0 Migrate Guide我尝试使用Response Serialization,但我遇到以下错误。我似乎无法弄清问题。我对ResponseCollectionSerializable也有同样的错误。

enter image description here

1 个答案:

答案 0 :(得分:2)

您应该使用符合GenericResponseSerializer的{​​{1}}:

ResponseSerializer

public protocol ResponseObjectSerializable { init?(response: NSHTTPURLResponse, representation: AnyObject) } extension Request { public func responseObject<T: ResponseObjectSerializable>(completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<T>) -> Void) -> Self { let responseSerializer = GenericResponseSerializer<T> { request, response, data in let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments) let result = JSONResponseSerializer.serializeResponse(request, response, data) switch result { case .Success(let value): if let response = response, responseObject = T(response: response, representation: value) { return .Success(responseObject) } else { let failureReason = "JSON could not be serialized into response object: \(value)" let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason) return .Failure(data, error) } case .Failure(let data, let error): return .Failure(data, error) } } return response(responseSerializer: responseSerializer, completionHandler: completionHandler) } } 是所有响应序列化程序必须符合的协议。