Storing Int64 in UserDefaults
A user default object can only be an instance (or a combination ofinstances) of NSData
, NSString
, NSNumber
, NSDate
, NSArray
, or NSDictionary
.
Some Swift types are automatically bridged to Foundation types,e.g. Int
, UInt
, Float
, Double
and Bool
are bridgedto NSNumber
. So this could be saved in the user defaults:
var teamsData = Dictionary<String,Dictionary<String,Int>>()
On 64-bit architectures, Int
is a 64-bit integer, but on32-bit architectures, Int
is a 32-bit integer.
The fixed-sized integer types such as Int64
are notautomatically bridged to NSNumber
. This was also observedin Swift - Cast Int64 to AnyObject for NSMutableArray.Therefore, to store 64-bit integers in the user defaults you haveto use NSNumber
explicitly:
var teamsData = Dictionary<String,Dictionary<String,NSNumber>>()// Example how to add a 64-bit value:let value : UInt64 = 123teamsData["foo"] = ["bar" : NSNumber(unsignedLongLong: value)]
Martin's answer is no longer correct for Swift 3 since the set function is now type Any?
instead of AnyObject?
.
You can store an Int64 in UserDefaults like so:
import Foundationlet value: Int64 = 1000000000000000UserDefaults.standard.set(value, forKey: "key")if let value2 = UserDefaults.standard.object(forKey: "key") as? Int64 { // value2 is an Int64 with a value of 1000000000000000 print(value2)}
You can paste the above code into a Swift playground and try yourself.
Swift 4:
You can save int64 as string in UserDefaults
let value: Int64 = 1000000000000000let stringValue = String(value)UserDefaults.standard.set(stringValue, forKey: "int64String")
Like that you avoid Int truncation.
And then you can recover the original value:
let int64String = UserDefaults.standard.string(forKey: "int64String")let originalValue = Int64(int64String!)
This allow you to compare it with other int64 values