Genhain
Genhain

Reputation: 1937

-[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.

Upvotes: 5

Views: 5974

Answers (1)

aleroot
aleroot

Reputation: 72636

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

Upvotes: 14

Related Questions