Reputation: 3156
I made a request to facebook API using facebook SDK to get user basic data. Everything works ok, but when I try to pass the facebook ID as a NSInteger number the returned number is wrong.
The facebook ID is: 100001778401161 But after convert the number to NSInteger the number returned is: 2054848393
How can I store the facebook ID on a NSInteger variable?
My current code is:
NSLog(@"The ID: %ld", (long)[[user objectForKey:@"id"] intValue])
Thanks.
Upvotes: 0
Views: 133
Reputation: 11643
You can't store the number larger than 2 ^ 31 in NSInteger
type.
If you want to store the one larger, then you can use NSDecimalNumber
object instead.
Or you can use unsigned long long
type.
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:[user objectForKey:@"id"]];
NSLog(@"Number: %@", number);
unsigned long long ullValue = strtoull([user objectForKey:@"id"], NULL, 0);
NSLog(@"Number: %llu", ullValue);
Hope this will help you!
Upvotes: 0
Reputation: 537
NSInteger (and long) is a 32-bit value and the ID you are using exceeds the maximum value that it can hold, so it overflows. You could try:
long long facebookId = [[user objectForKey: @"id"] longLongValue];
NSLog(@"The ID: %lld", facebookId);
I don't know anything about the facebook API so you might want to make sure that the ID is guaranteed to be numeric over time. If, for example, they specify somewhere that the ID is a string, you should match that even if they seem to always be numeric.
Upvotes: 0
Reputation: 432
Such a number needs 64 bits, NSInteger does only cover 32 bits (and with positive numbers only 31 bits). Try using long long values:
NSLog(@"The ID: %lld", [[user objectForKey:@"id"] longLongValue]);
you can use also NSNumber if you need to store it as an object somehow:
NSNumber *number=[NSNumber numberWithLongLong:[user[@"id"] longLongValue]];
Upvotes: 3