If you decode a data type with the wrong NSCoding function, the app crashes. For example, this line will crash if DoubleKey
actually represents a double and not an integer as expected by decodeInteger
:
let isReallyDouble = aDecoder.decodeInteger(forKey: DoubleKey)
How do you verify the data type before decoding? This is important if version 1 of the app encodes some value as an integer but version 5 must change that variable to a double.
This is for Swift 3, where decodeObject
doesn't work for value types like integer and double: Swift 3 saving and retrieving custom object from userDefaults
Resulting in a crash, instead of returning nil
, when the data type is mismatched seems like an awfully poor design choice.