I would suggest changing this web service to return values ββsequentially (and if there is no value for the integer type, do not return anything for this key).
But if you're stuck with this design, you'll have to write your own init(from:) , which gracefully handles rejection of parsing an integer value. For example:.
struct Person: Codable { let name: String let age: Int? init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) name = try values.decode(String.self, forKey: .name) do { age = try values.decode(Int.self, forKey: .age) } catch { age = nil } } }
I would also recommend using 0 as a reference value for "no integer value provided". This requires options.
Rob
source share