I have a struct UISectionModel
with only one property section
, which has a protocol UISectionProtocol
for a type, as follows:
struct UISectionModel: Codable {
let section: UISectionProtocol
}
Here is the implementation of the UISectionProtocol
protocol:
protocol UISectionProtocol: Codable {
var identifier: String? { get }
}
As you can see, I need UISectionModel
to conform to the Codable
protocol, but I keep getting an error stating that it does not conform to neither Decodable
or Encodable
, even though the only property's type is already conforming to Codable
. I suspect it has to do with the fact that this type is a protocol, but I can't figure the exact reason that is causing the error to appear or how to fix it.
Would appreciate some insight!
I tried replacing the type of section
with an enum that conforms to UISectionProtocol
:
struct UISectionModel: Codable {
let section: UIDefaultSection
}
enum UIDefaultSection: UISectionProtocol {
case imageSection(section: UIImageSection)
case labelSection(section: UILabelSection)
var identifier: String? {
switch self {
case .imageSection(let section): return section.identifier
case .labelSection(let section): return section.identifier
}
}
}
This made the error go away, but I'm thinking that section
needs to be of any type that conforms to UISectionProtocol
, instead of being limited only to UIDefaultSection
.