Q:

NSNumbers changing

I have an array of NSNumbers I am saving into Core Data entities.
I have set their NSNumber property I want to set to Integer64.
On some of the numbers I get strange results and am wondering if it is a type issue.
I have a temporary object I fill with data that gets extracted into the Core-Data type object on creation.
e.g. [cachableObject setValue:tempObject.number forKey:@"number"];
I place two log statements, one above, the other below this line.
NSLog(@"tempObject.number is %@", tempObject.number);
[cachableObject setValue:tempObject.number forKey:@"number"];
NSLog(@"cachableObject.number is %@", cachableObject.number);

This logs the following on the two incorrect occurrences(so far):
tempObject.number is 14047556750440521185
cachableObject.number is -4399187323269030431

tempObject.number is 12267813409388115511
cachableObject.number is -6178930664321436105

What is causing this problem? On other numbers, the numbers dont change.

A:

14047556750440521185 = 0xC2F2F26FF825DDE1

is greater than 2^63 - 1 and exceeds the range of a signed 64-bit integer number. You can store it in a unsigned 64-bit integer, but when interpreted as signed number, the result is -4399187323269030431.
Core Data uses signed 64-bit numbers for an Integer 64 attribute, and even if you assign it a unsigned 64-bit number, the value is internally stored as signed 64-bit number.
If you know that the value is an unsigned number, you can use
unsigned long long x = [cachableObject.number unsignedLongLongValue];

to convert it to an unsigned value again.

