I have the following struct:
struct ImageSlider: CodableComponent {
let slides: [CodableComponent]?
}
which conforms to CodableComponent
protocol Component {
}
typealias CodableComponent = Component & Decodable
But I get the following error:
Type 'ImageSlider' does not conform to protocol 'Decodable'
As soon as I remove the conformance to CodableComponent in ImageSlider, like the following:
struct ImageSlider {
let slides: [CodableComponent]?
}
The error is gone.
I'm not sure what exactly is happening here, why I cannot conform to a protocol and also have a property of the same type?
Would be appreciated if someone could shed some light on this case.
CodePudding user response:
You have to implement Decodable protocol, for example:
struct ImageSlider: CodableComponent {
let slides: [CodableComponent]?
init(from decoder: Decoder) throws {
// decode values here
}
}
CodePudding user response:
So, I found out that there are 2 ways to address this issue. The first one is using init(from:)
as you can see in the comments and other answers. The other one is by using "existential any" in Swift 5.6
struct ImageSlider: any CodableComponent {
let slides: [any CodableComponent]
}