Aditya Deshmane
Aditya Deshmane

Reputation: 4722

Setting NSNumber to NSManagedObject attribute

I have Entity with attribute "id" in my model with data type integer 64. I m using property @property (nonatomic, retain) NSNumber * id; for it. I create NSManagedObject using

id tempItem = [NSEntityDescription insertNewObjectForEntityForName:@"XYZ" 
                inManagedObjectContext:context];

and setting NSNumber myID value [tempItem setValue:myID forKey:@"id"];

Problem is: when I print NSNumber myID using print description it shows me correct number but when I set it to my NSManagedObject using the code above and print NSManagedObject it goes into negative value. I'm trying to set 12941051589483540916 (this number is a valid interger64), which gets set to -5505692484226010700.

What am I doing wrong ?

Thanks in advance!

Upvotes: 0

Views: 551

Answers (2)

Martin R
Martin R

Reputation: 539745

12941051589483540916 is not a valid "Integer 64". It is greater than

2^63 - 1 = 9223372036854775807

and therefore does not fit into the range of a signed 64-bit number.

Core Data "Integer 64" is a signed 64-bit number, therefore

12941051589483540916 = 0xB397D9DB232BB1B4

is interpreted as negative number -5505692484226010700.

If you know that the values stored in the managed object should represent unsigned quantities, you have to cast them to an unsigned type, e.g.

uint64_t theId = [[tempItem valueForKey:@"id"] unsignedLongLongValue];

Upvotes: 1

Luca Bernardi
Luca Bernardi

Reputation: 4199

It is a very bad idea use id as property name. id is a type, namely a generic pointer to a generic object and I think that this create more than one issue, now or in the future.

In Core Data, I usually create the unique identifier property with the following format ID, i.e. for the class Post i will have @property (nonatomic, retain) NSNumber *postID;

Upvotes: 2

Related Questions