1

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.

Peter Kazazes
  • 3,600
  • 7
  • 31
  • 60

1 Answers1

4

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

joern
  • 27,354
  • 7
  • 90
  • 105
  • 1
    try a "long long". NSString has [debtString longLongValue] – joern Jul 19 '11 at 16:04
  • `int debtInt = [debtString longLongValue]; NSLog(@"Integer form: %lli", debtInt);` returns `2011-07-19 12:31:52.396 National Debt[51367:207] String form: 14342943000000 2011-07-19 12:31:52.397 National Debt[51367:207] Integer form: 432424662392489408` – Peter Kazazes Jul 19 '11 at 16:32
  • 2
    @Peter - your converting the string correctly now, but then storing it back into an int, which is too small for it. Try long long debtInt = [debtString longLongValue]; instead. – Perception Jul 19 '11 at 16:33