Consider the followings:
protocol A: Codable {
var b: B { get }
var num: Int { get }
}
protocol B: Codable {
var text: String { get }
}
struct C: A {
var b: B
var num: Int
}
The compiler gives two errors
- Type 'C' does not conform to protocol 'Decodable'
- Type 'C' does not conform to protocol 'Encodable'
However Both A and B are Codable. How to solve/avoid these errors?
EDITED
As the auto-synthesis for Codable
not working, I manually implemented the required methods.
struct C: A {
var b: B
var num: Int
enum CodingKeys: String, CodingKey {
case b
case num
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(num, forKey: .num)
try container.encode(b, forKey: .b)
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
num = try values.decode(Int.self, forKey: .num)
b = try values.decode(B.self, forKey: .b)
}
}
and now it gives new errors