Before Swift 3, you decode boolean values with NSCoder like this:
if let value = aDecoder.decodeObjectForKey(TestKey) as? Bool {
test = value
}
The suggested approach in Swift 3 is to use this instead:
aDecoder.decodeBool(forKey: TestKey)
But the class reference for decodeBool
doesn't explain how to handle the situation if the value you're decoding isn't actually a boolean. You can't embed decodeBool
in a let statement because the return value isn't an optional.
How do you safely decode values in Swift 3?