Type "X" does not conform to protocol "ResponseObjectSerializable"

I am trying to serialize custom responses with Alamofire

I follow what is written in the README and create a protocol and an extension

@objc public protocol ResponseObjectSerializable {
    init?(response: NSHTTPURLResponse, representation: AnyObject)
}

extension Alamofire.Request {
    public func responseObject<T: ResponseObjectSerializable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, NSError?) -> Void) -> Self {
        let serializer: Serializer = { (request, response, data) in
            let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
            let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data)
            if response != nil && JSON != nil {
                return (T(response: response!, representation: JSON!), nil)
            } else {
                return (nil, serializationError)
            }
        }

        return response(serializer: serializer, completionHandler: { (request, response, object, error) in
            completionHandler(request, response, object as? T, error)
        })
    }
}

      

but when i try to execute it i got this error. The type "my_model_class" does not conform to the protocol "ResponseObjectSerializable"

My model is just a boneless class

final class Shot: ResponseObjectSerializable {
    required init?(response: NSHTTPURLResponse, representation: AnyObject) {
    }
}

      

Use this with Xcode 6.3, has anyone experienced this? and know how to make this work?

answer to @sidepeed The error goes away, but what confuses me in the Apple Swift doc, they have an example in the protocol @objc

, and the swift class that matches it is not required@objc

@objc protocol CounterDataSource {
    optional func incrementForCount(count: Int) -> Int
    optional var fixedIncrement: Int { get }
}

class TowardsZeroSource: CounterDataSource {
    func incrementForCount(count: Int) -> Int {
        if count == 0 {
            return 0
        } else if count < 0 {
            return 1
        } else {
            return -1
        }
    }
}

      

+3


source to share


1 answer


Shot

is not marked as @objc

, unlike the protocol, so yours init

doesn't meet the requirement:

@objc public protocol ResponseObjectSerializable {
    init?(response: NSHTTPURLResponse, representation: AnyObject)
}

final class Shot: ResponseObjectSerializable {
    @objc required init?(response: NSHTTPURLResponse, representation: AnyObject) {
    }
}

      

results in an error:



note: the protocol requires an initializer of init(response:representation:)

type(response: NSHTTPURLResponse, representation: AnyObject)

  init?(response: NSHTTPURLResponse, representation: AnyObject)`
  ^

      

note: no candidate @objc

, but it requires a protocol

Attach @objc

before the definition Shot

and it should compile.

+2


source







All Articles