Peter Kazazes
Peter Kazazes

Reputation: 3628

Issue Converting NSString to Int

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.

Upvotes: 1

Views: 409

Answers (1)

joern
joern

Reputation: 27620

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

Upvotes: 4

Related Questions