'How do you store an UInt64 in CoreData?
My understanding is that a UInt64 can be any value from: 0 to 18446744073709551615
I need to save a UInt64 identifier to CoreData, however the values I see are:
I initially tried Integer 64 but now I am understanding that has a range of: -9223372036854775808 to 9223372036854775807
Do developers usually store an UInt64 as a String and do conversions between the two types? Is this the best practice?
Solution 1:[1]
The accepted answer by Martin R will work.
However it appears that this is part of the default implementation of @NSManaged properties by Apple.
I tested it like so:
- I created a new Xcode project with Core Data and made a single Entity called
Hello. - It has a single attribute called
shortwhich is saved as anInteger 16. - Marked Codegen as
Manual/None - Created the manual class file for
Helloas follows:
class Hello: NSManagedObject {
@NSManaged var short : UInt16
}
You'll notice that I've typed it as an unsigned UInt16.
In my AppDelegate:
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Get Context
let context = self.persistentContainer.viewContext
// Create Test Objects
let hello1 = Hello(entity: Hello.entity(), insertInto: context)
hello1.short = 255
let hello2 = Hello(entity: Hello.entity(), insertInto: context)
hello2.short = 266 // Should overflow Int16
let hello3 = Hello(entity: Hello.entity(), insertInto: context)
hello3.short = 65535 // Should overflow Int16 by a lot
// Save them to the database
do {
try context.save()
} catch {
print(error)
}
// Fetch the save objects
let fetch = NSFetchRequest<Hello>(entityName: "Hello")
if let results = try? context.fetch(fetch) {
for result in results {
print(result.short)
}
}
}
This prints out the following:
255
266
65535
Which I imagine is what someone would want from saving unsigned ints in Core Data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JeremySom |

