I'm working on a project (Swift 4) and I want to save an array of custom Objects which are displayed on a table view. To do that, I have to adopt the Codable protocol. But I have to adapt my subclass too, and frankly I do not know how. Still a beginner and trying to learn that stuff.
Here some example code, which should represent my problem.
//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?
}
}
I just don't know how to properly adapt the Subclass to the Codable protocol in order for it to work.