问题将NSString转换为Int

时间:2011-07-19 15:46:23

标签: objective-c nsstring integer

出于某种原因,当我将此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值上,则会出现无效摘要。我能想到的唯一原因就是我在转换之前释放了debtString,但这显然不是真的。

1 个答案:

答案 0 :(得分:4)

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

相关问题