Decoding a dictionary with enum as key and dropping a case

Because Swift encodes and decodes a dictionary using an enum as the key into an array with alternated values (as explained here: JSON Encoding / Decoding weird encoding of dictionary with enum values and coded here:, how are we supposed to handle Decodable when we drop a case or rename it?

Because the local data will have an obsolete key and therefore the decode function will fail. But if I try to decode a [String: MyObject] instead of [MyEnum: MyObject], then I will get an error about how the dictionary was actually saved as a string with alternated values.

Is my only option to never drop any case of the enum?

Speaking just more abstractly for a moment, why would you expect this to not fail? The encoding/decoding model is generally not concerned with versioning — for all data types, not only dictionaries.

Enums as data types in Swift are treated as opaque unless they declare a raw value (this allows them to have associated values, for example), so here they behave like any other struct, and their synthesized Codable conformance is a mere convenience that makes no guarantees about the actual encoding format — so if you change a type as a whole, no matter how drastically, you cannot rely on it being able to assemble itself with some old data.

If you are relying on the synthesized conformance — yes, for the above reason.

I expect it to fail, but I don't expect it to not give a single way to handle the data (which isn't some unreadable binary but just plain String as an Enum).

I know that encoding/decoding doesn't handle versioning, it's not their responsibility but mine. However, Swift language doesn't give me any tool for that in this very specific case. If I knew that Enum was a pain to handle when adding a removing a case, I would never make it Codable in a Dictionary - because the way it is handled behind the scene doesn't feel natural in Swift.

It is easy to handle format changes in all the other cases, but not this one as the way Swift encodes/decodes data of Dictionary with Enum keys isn't explicit.

Anyway, I'll just keep my legacy cases for the moment... :confused: