- [NSString intValue] returns a completely different value than the string

I have a numeric string and there is a discrepancy where it is intValuevery different from its string value. Code below:

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

gives the result:

"ID 100004378121454"
"ID as integer 2147483647"

The only logical guess I can make is that the string is too long to convert it to int... In any case, I tried longLongValue, etc. - to different results, but not to those that should be.

+5
source share
1 answer

Your number (100004378121454) is more than a number than a simple int can handle, you should use a type long longin this case (so as not to remove your number to the maximum value of int32):

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

Output:

ID              100004378121454
ID as long long 100004378121454
+14
source

All Articles