phil
phil

Reputation: 1003

crash when using NSNumberFormatter to convert NSString to NSNumber

I have a simple value in an NSSTRING that I want to convert to an NSNumber. I do this all the time in my code and for some reason, this time it is not working. Do you see anything wrong with this?

NSNumberFormatter * num_formatter = [[NSNumberFormatter alloc] init];
[num_formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *score;
NSString *mystr = [[player ScoresArray] objectAtIndex:currentKeeper - 1];


NSLog(@"here is my string: -%@-", mystr);
score = [num_formatter numberFromString:mystr];   // crash occurs on this line.  see error below...
NSLog(@"now it is: %d", [score intValue]);        // it never gets to this line...

Here is the output from the above code:

here is my string: -3-

Here is the error I get:

2013-02-26 17:21:48.912 Golf Whiz[50407:c07] -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xcb5fa90
2013-02-26 17:21:48.912 Golf Whiz[50407:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xcb5fa90'
*** First throw call stack:

Upvotes: 0

Views: 1217

Answers (2)

phil
phil

Reputation: 1003

Thanks, all, as suspected, it turns out that I did have nsnumber objects in the scoresArray afterall.

Upvotes: 0

Mike C.
Mike C.

Reputation: 601

Make sure the objects you're adding to ScoresArray are in fact strings, not NSNumbers.

Upvotes: 0

Related Questions