What is the recommended way to assign an optional Swift property a decoded primitive with `NSCoder`?

For an example like:

someOptionalDouble = aDecoder.decodeDouble(forKey: "SomeKey")

I'm not sure how to avoid a crash if the double was encoded as nil, while also avoiding Apple's console warning that decoding primitives as an object will become an error in the future.

I’m not sure if this is the best way to do this, but one option is to preflight the decode with containsValue(forKey:).

extension NSCoder {
    
    func decodeOptionalDouble(forKey key: String) -> Double? {
        guard self.containsValue(forKey: key) else { return nil }
        return self.decodeDouble(forKey: key)
    }
}

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

What is the recommended way to assign an optional Swift property a decoded primitive with `NSCoder`?
 
 
Q