简体   繁体   中英

Saving and retrieving data from core data

I have a program where I save an int value as follows:

client.oldId = [clientDict objectForKey:@"oldId"];

At this point when debugging the value that is saved "supposely" is 31.

When I attempt to retrieve this item using the context, and i retrieve it as follows:

 int oldId = [NSNumber numberWithInt:client.oldId];

It becomes 174806944, why could this happen?, there are no summation operations or anything like that for this item and right before it saves to the context it is 31.

Any help would be appreciated.

set your variable to 0 before you set it

int oldId = 0
oldid = [NSNumber numberWithInt:client.oldId]; 

sometimes you can get garbage in a memory location that can cause this.

Updated Answer: Though technically sometimes I have found that if you dont clear out the integer I get the same thing. In this case he was setting an int to an NSNumber . The correct way was to take the NSNumber and get its integerValue like this:

[client.oldId intValue]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM