dgee4
dgee4

Reputation: 381

Swift NSCoder Not Working For Double Variable

I'm restoring the state of my app and am storing a few variables using the NSCoder in Xcode 10.1. The NSCoder is fine at storing strings and other variable types, but won't allow me to store Doubles. I've tried numerous scenarios but it just doesn't seem to like Doubles!

If I convert the double into a string it will work. I've tried 'as? Double' and this still returns nil. Any ideas why I just can't use a Double?

override func encodeRestorableState(with coder: NSCoder) {
    ...
    // Both don't work:
    coder.encode(myDoubleVariable, forKey: "Length")
    coder.encode(1.2345252, forKey: "Length")
}

override func decodeRestorableState(with coder: NSCoder) {
    ... 
    // Both don't work:
    myDoubleVariable = coder.decodeObject(forKey: "Length") 
    myDoubleVariable = coder.decodeDouble(forKey: "Length") 
}

Upvotes: 0

Views: 84

Answers (0)

Related Questions