I'm trying to make inherited data model in order to parse it with JSONDecoder
.
class FirstClass : Codable {
let firstClassProperty: Int
final let arrayOfInts: [Int]
}
class SecondClass : FirstClass {
let secondClassProperty1: Int
let secondClassProperty2: Int
private enum CodingKeys : String, CodingKey {
case secondClassProperty1, secondClassProperty2
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
secondClassProperty1 = try container.decode(Int.self, forKey: .secondClassProperty1)
secondClassProperty2 = try container.decode(Int.self, forKey: .secondClassProperty2)
try super.init(from: decoder)
}
}
I use this JSON for FirstClass
:
{
"firstClassProperty": 123,
"arrayOfInts": [
123
]
}
and this for SecondClass
:
{
"firstClassProperty": {},
"secondClassProperty1": {},
"secondClassProperty2": {}
}
How can I get rid of arrayOfInts
in my subclass but let it be in superclass if keyword final
doesn't work in this case?
Here's Playground. Thanks for your answers!