JSONDecoder - missing key - won't read data

I store the data for my app in UserDefaults using JSONEncoder and read it back using JSONDecoder.

Everything works fine until I add another field to the struct that describes the data. When I start the app, the JSONDecoder tries to read the new field, which doesn’t yet exist in the data. The error is ‘keyNotFound’ and it causes the JSONDecoder to abort without returning any data.

Is there a way around this? I would like to read the data that doesn’t have this new field, then, when I save it, the new field will be there. This seems like a fairly common situation.

Make the new field Optional, so it’s okay for it to be absent when you read in the data. Then, when you write out the data, that field will be written out if it is present.

That was easy. Thank you.