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
Either you get everything for free but standardized or you have to write a custom initializer like
struct MyStruct: Codable {
let id: Int
var name: String
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
let decodedName = try container.decode(String.self, forKey: .name)
name = "\(id) \(decodedName)"
}
}
You can implement init()
but this works independent of the decoding functionality and you have to assign a default value to all non-optional properties, that's what the error says.