With in Server response sometimes missing "tags" key. How we have to write codable struct for this response.
[
{
"product_id": 10
"product_name": "Bulb"
"tags": ["x", "y", "z"]
},
{
"product_id": 11
"product_name": "Wire"
}
]
decoding like this
do {
// decoding...
let product_model = try JSONDecoder().decode([ProductItem].self, from: data)
} catch let error {
print("Product list error(decoder): \(error.localizedDescription)")
}
// Product structure
struct ProductItem: Codable {
// variables
let product_id: String?
let product_name: String?
let tags: [String]?
// alternative keys...
private enum CodingKeys: String, CodingKey {
case product_id
case product_name
case tags // what i have to do here
}
}