Converting string to int changes the value

牧云@^-^@ 提交于 2019-11-28 12:52:59

问题


I'm trying to convert a string value I retrieved from an XML parser into an integer. Unfortunately, I'm finding that the integer value is not reflecting the number in the string, and I can't figure out why. Here is my code:

int maxTweetID = [[[_dataArray lastObject]tweetID]intValue];

NSLog(@"String Value of tweet ID: %@",[[_dataArray lastObject]tweetID]);
NSLog(@"Int Value of tweet ID: %i",[[[_dataArray lastObject]tweetID]intValue]);

and here is what I'm getting from the debugger:

2012-05-15 10:58:44.395 nearYou[2460:707] String Value of tweet ID: 202425636143370242
2012-05-15 10:58:44.396 nearYou[2460:707] Int Value of tweet ID: 2147483647

Why is this?


回答1:


The number is much too large to fit in a 32 bit int. What you want to do is use a long instead:

long maxTweetID = [[[_dataArray lastObject]tweetID]longValue];

EDIT:

You actually need to use long long in Objective-C:

long long maxTweetID = [[[_dataArray lastObject]tweetID]longLongValue];



回答2:


The number in the string is too large to be stored as an integer. The highest integer possible with 32 bits is 2.147.483.647. Try using a long.



来源:https://stackoverflow.com/questions/10606281/converting-string-to-int-changes-the-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!