我正在尝试制作继承的数据模型以便用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)
}
}
我将此 JSON 用于FirstClass
:
{
"firstClassProperty": 123,
"arrayOfInts": [
123
]
}
这对于SecondClass
:
{
"firstClassProperty": {},
"secondClassProperty1": {},
"secondClassProperty2": {}
}
如果关键字在这种情况下不起作用,我怎样才能摆脱arrayOfInts
我的子类但让它在超类中?final
这里是游乐场。感谢您的回答!