由於某些原因,當我將此NSString轉換爲整數時,我得到一個完全隨機(但一致)的數字。問題將NSString轉換爲Int
- (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
,但這顯然不是真的。
是否有可能我們的國債已經超過了無符號整數的存儲容量!?那麼我會被詛咒。 – Perception
http://stackoverflow.com/questions/2107544/types-in-objective-c-on-iphone – albertamg