How do I make a Codable class use decodeIfPresent for all properties without needing to type out all properties in a custom initializer?
An example:
class Book: Codable {
var name: String = "Default name"
var pages: Int = 1
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
for key in container.allKeys {
// What to do here to call decodeIfPresent?
}
}
}
In essence, I'd like to replicate the automatically created initializer myself, making this one change to how it works.