Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-[NSString intValue] returning completely different value than the string says

I have a numeric string and have observed a discrepency where its intValue is vastly different from its string value. The code below:

NSString *string = [result objectForKey:@"id"];
NSLog(@"ID %@",string);
NSLog(@"ID as integer %i",[string intValue]);

gives an output:

"ID 100004378121454"
"ID as integer 2147483647"

The only logical guess I can make is that the string is too long to be converted to an int... In any case I tried longLongValue and such - to different results but not the one it should be.

like image 980
Genhain Avatar asked Sep 29 '12 09:09

Genhain


1 Answers

Your number(100004378121454) is greater number than a simple int can handle, you have to use long long type in this case(to not get your number truncated to the int32 maximum value) :

NSString *string = @"100004378121454";
NSLog(@"ID %@",string);
NSLog(@"ID as long long %lli",[string longLongValue]); 

Output :

ID              100004378121454
ID as long long 100004378121454
like image 139
aleroot Avatar answered Sep 28 '22 09:09

aleroot