So, I'm trying to achieve this:
Have a protocol with associatedtype who will handle json parsing into his extension. The associatedtype must conform to Decodable
:
protocol MyProtocol {
associatedtype ResponseType: Decodable
func handleResponse(data: Data) -> ResponseType
}
What I'm want to do is to set a default type for responseType into my extension and then, if needed, override that type into the class, or struct conformance. Something like this.
extension MyProtocol {
typealias ResponseType = MyDefaultDecodableType
func handleResponse(data: Data) -> ResponseType { ... }
}
class MyObject: MyProtocol {
typealias ResponseType = AnotherDecodableType
}
Problem is I'm getting an error like this inside MyObject
:
error: type 'MyObject' does not conform to protocol 'MyProtocol'
class MyObject: MyProtocol {
^
note: multiple matching types named 'ResponseType'
associatedtype ResponseType: Decodable
^
note: possibly intended match
typealias ResponseType = AnotherDecodableType
^
note: possibly intended match
public typealias ResponseType = MyDefaultDecodableType
I don't know if it's possible to achieve what I'm trying or I'm approaching the wrong way. Anyone can give me some light?
Thanks.
Decodable
protocol ? – Prashant Tukadiya