1

This is my first time using NSCoder and there used to be a method called encodeInteger but it seems to have vanished in Swift 3 and the docs don't seem to help.

It maybe that the confusion lies in the difference between Int and UInt64. Are they the same?

Should i be using a NSKeyedArchiver and if so how does that work to comply with NSCoding?

Here's before with the error: enter image description here And after without an error: enter image description here

aaronium112
  • 2,992
  • 4
  • 35
  • 50

1 Answers1

1

Why don't you use NSNumber and encode it as an object? It'd look like this:

let bigNumber: UInt64 = /* 123 */
let number = NSNumber(value: bigNumber)

// Encoding it just like a String    
coder.encode(number, forKey: "BigNumberKey")

// Decoding and using the property uint64Value from NSNumber to get the UInt64 back
if let object = coder.decodeObject(forKey: "BigNumberKey") as? NSNumber {
    let decodedBigNumber = object.uint64Value
}

If that's a requirement for some reason, NSCoder supports the encoding of Int64 (and you could cast it, described here).


The change from encodeInteger to just encode is part of SE-0005 (which affected a lot of different classes; UIColor.blueColor() is now UIColor.blue(), for instance).

Community
  • 1
  • 1
Alberto S.
  • 81
  • 6