Thellimist
Thellimist

Reputation: 4007

Cannot specialize non-generic type ResponseSerializer

From the documentation and the 2.0 Migrate Guide I tried to use Response Serialization but I'm having the following errors. I can't seem to figure out the problems. I'm also having the same errors with ResponseCollectionSerializable.

enter image description here

Upvotes: 0

Views: 1889

Answers (1)

mixel
mixel

Reputation: 25846

You should use GenericResponseSerializer that conforms to 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)
    }
}

ResponseSerializer is the protocol in which all response serializers must conform to.

Upvotes: 2

Related Questions