I have a NSMutableArray allAnswers
NSMutableArray *allAnswers = [[NSMutableArray alloc] init];
With the values :
allAnswers --- (
{
PlayerName = Trfghuhhrf;
Score = 1000;
},
{
PlayerName = Test;
Score = 333;
},
{
PlayerName = DDDDDDDD;
Score = 250;
},
{
PlayerName = SSSSSSSSSSSSSSS;
Score = 100;
},
{
PlayerName = mn;
Score = 100;
}
)
This is how I am building dictionary.
NSDictionary *answer = [NSDictionary dictionaryWithObjectsAndKeys:
name, PlayerKey,
[NSNumber numberWithInt:[score intValue]], ScoreKey,
nil];
[allAnswers addObject:answer];
NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:ScoreKey ascending:NO];
[allAnswers sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
NSLog(@"allAnswers --- %@",allAnswers);
for (NSDictionary *answer in allAnswers)
{
NSLog(@"%@, %@", [answer objectForKey:PlayerKey], [answer objectForKey:ScoreKey]);
NSMutableString *mString = [[NSMutableString alloc]init];
[mString appendString:[answer objectForKey:PlayerKey]];
[mString appendString:@" - "];
[mString appendString:[answer objectForKey:ScoreKey]];
NSLog(@"mString --******-- %@",mString);
}
App is crahsing when I am trying to add [answer objectForKey:ScoreKey] to mutable string. I tried converting [answer objectForKey:ScoreKey] to (NSString) but that did’t help.
Please advise what I am doing wrong.
Sorry for long description.
Adding Crash logs :
2012-03-03 00:43:48.149 NumberTwins[38378:10703] -[__NSCFNumber length]: unrecognized selector sent to instance 0x9155400
2012-03-03 00:43:48.149 NumberTwins[38378:10703] * Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[__NSCFNumber length]: unrecognized selector sent to instance 0x9155400’
* First throw call stack:
You are storing an
intas anNSNumberfor the keyScoreKeybut you can’t just append this to a string, because it is the wrong type. You need to convert it to anNSString.You can’t just cast the
NSNumberto anNSStringbecause its a different type altogether; you should use the correct method of NSNumber:The reason you are able to get a value logged is because putting a
%@in the log calls the object’sdescriptionmethod. This method is declared in NSObject, and the Framework classes implement it so that it returns a representation of the object as a string.