As I am learning about the Codable
protocol of Swift 4 and thinking about how this can be used, I want to understand what are the implications on the potential future evolution of a type when making it Codable
.
In usual code, if I change a type by adding a property to it, I just need to update my local codebase to react to this change (by adding a new argument to my type initialiser calls for instance).
On the other hand, if my type had been Codable
and I had already used it in combination with a distant server (by sending and receiving this type encoded as JSON) wouldn't it be pretty problematic to add new properties ?
For instance, say that I receive a JSON encoded by the previous version of my app (that was still stored by the server), and I try to decode it with the new version, the decoding process will fail as the data for the new property won't be available.
How should I deal with such situations ?
"Locking" my type at some point, forbidding it to evolve in the future ?
What would be the cleanest way to decode incompatible data if it was really needed ?