我正在开发一个项目(Swift 4),我想保存一个显示在表视图上的自定义对象数组。为此,我必须采用Codable协议。但我也必须调整我的子类,坦率地说我不知道如何。仍然是初学者,并试图学习那些东西。
这里有一些示例代码,应该代表我的问题。
//The first class of Objects I want to save
class Vehicle: Codable {
let wheels: Int
let color: String
init(wheels: Int, color: String) {
self.wheels = wheels
self.color = color
}
}
//The second class of Objects I want to save
class Car: Vehicle {
let manufacturer: String
init(wheels: Int, color: String, manufacturer: String) {
self.manufacturer = manufacturer
super.init(wheels: wheels, color: color)
}
//this method is needed when I implement the Codable protocol to the super class
required init(from decoder: Decoder) throws {
//What should I do here?
}
}
我只是不知道如何正确地使Subclass适应Codable协议以使其工作。