将 NSString 转换为 Int 时出现问题

发布于 2024-11-24 22:35:40 字数 1092 浏览 0 评论 0原文

由于某种原因,当我将此 NSString 转换为整数时,我得到一个完全随机(但一致)的数字。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    // release the connection, and the data object
    [connection release];

    debtString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

    NSLog(@"String form: %@", debtString);
    [receivedData release];

    int debtInt = [debtString intValue];
    NSLog(@"Integer form: %i", debtInt);

}

这是我的控制台输出:

2011-07-19 11:43:18.319 National Debt[50675:207] Succeeded! Received 14 bytes of data
2011-07-19 11:43:18.320 National Debt[50675:207] String form: 14342943000000
2011-07-19 11:43:18.320 National Debt[50675:207] Integer form: 2147483647

如果我在 NSLog(@"Integer form: %i", DebtInt); 行上放置一个断点,并将鼠标悬停在 debtString 值上在上面的行中,有一个无效的摘要。我能想到发生这种情况的唯一原因是我在转换之前释放了债务字符串,但这显然不是真的。

For some reason when I convert this NSString to an integer, I get a completely random (yet consistent) number.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    // release the connection, and the data object
    [connection release];

    debtString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

    NSLog(@"String form: %@", debtString);
    [receivedData release];

    int debtInt = [debtString intValue];
    NSLog(@"Integer form: %i", debtInt);

}

and this is my console output:

2011-07-19 11:43:18.319 National Debt[50675:207] Succeeded! Received 14 bytes of data
2011-07-19 11:43:18.320 National Debt[50675:207] String form: 14342943000000
2011-07-19 11:43:18.320 National Debt[50675:207] Integer form: 2147483647

If I put a breakpoint on the NSLog(@"Integer form: %i", debtInt); line, and hover over the debtString value in the line above, there is an invalid summary. The only reason I can think of for that happening is that I am releasing the debtString before it is converted, however this is obviously not true.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-12-01 22:35:40

字符串的 int 值高于 int 的最大值。因此它显示 int 的最大值,即 2.147.483.647

The int value of your string is higher than the maximum value of an int. Therefor it displays the max value of int which is 2.147.483.647

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文