0

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") 
}
dgee4
  • 381
  • 3
  • 16
  • Define "don't work". And why not use `Codable` and avoid all of the Objective-C baggage of using `NSCoder`? – rmaddy Nov 01 '18 at 21:26
  • @rmaddy I can't use Codable in this scenario. The Double variable always returns nil on decodeRestorableState – dgee4 Nov 01 '18 at 21:33
  • https://developer.apple.com/documentation/uikit/uiviewcontroller/1621461-encoderestorablestate – dgee4 Nov 01 '18 at 21:41
  • https://developer.apple.com/documentation/uikit/uiviewcontroller/1621429-decoderestorablestate – dgee4 Nov 01 '18 at 21:41
  • They are both methods within UIViewController fro restoring state-related information for a view controller. – dgee4 Nov 01 '18 at 21:42
  • Have you verified they are actually being called? – rmaddy Nov 01 '18 at 21:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182965/discussion-between-dgee4-and-rmaddy). – dgee4 Nov 01 '18 at 21:48
  • Yeah definitely being called – dgee4 Nov 01 '18 at 22:07

0 Answers0