Reputation: 23
I have a protocol A and there are multiple structs conforming to protocol A. I need to store different struct objects in a collection. But my collection type is predefined by another service, which is of type - class Storage<Value:Codable>. I can't pass in Value type as A, and it throws an error saying Protocol A does not conform to type Decodable. I just want to know if this is a right approach passing a custom protocol and can i make a custom protocol conform to Codable Protocol. Sample code snippet.
Any thoughts would be helpful
class Storage<Value: Codable>{}
protocol A {}
struct Type1: A, Codable, Equatable, Comparable {}
struct Type2: A, Codable, Equatable, Comparable {}
let storage = Storage<A>() // Throws error - Type 'A' does not conform to protocol 'Decodable'
Upvotes: 0
Views: 96
Reputation: 679
use basic class
class Storage<Value: Codable>{}
protocol A {}
class Type: A, Codable {}
class Type1: Type, Equatable, Comparable {}
class Type2: Type, Equatable, Comparable {}
let storage = Storage<Type>()
Upvotes: 1