I would like to do some initialization logic after the Swift Coding/Encoding feature has finished decoding a JSON.
struct MyStruct: Codable {
let id: Int
var name: String
init() {
name = "\(id) \(name)"
}
}
But I get the compiler error:
Return from initializer without initializing all stored properties
Which is clear to me because init() wants me to initialise all properties. But adding an init() with all needed properties also doesn't solve it because this initializer is not called(!) when Codable kicks in:
init(id: Int, name: String) {
// This initializer is not called if Decoded from JSON!
self.id = id
self.name = "\(id) \(name)"
}
Nevertheless - is there a way to do some initialisation logic after the Decoding has finished but without doing all the decoding manually for each property? So without implementing every time init(from decoder: Decoder)
. In this short example I have just two simple properties but production code consists of thousands of them.
Thanks.