Tag Archives: JSON

Handling Swift JSONDecoder typeMismatch error while using Codable protocol

If you have a problematic JSON, which can contain number or string for some keys, you can decode object without that property and manually set that property after decoding.

For example, I have a Vehicle class inside HistoryItem. In Vehicle model_year can be empty String or non empty Int. Here I am decoding modelYear manually using NSDictionary and trying to get Int. Swift 4 cannot do it automatically.

This is my Vehicle class that is contained inside HistoryItem:

As you see, there is no model_year key in CodingKeys.