I tried using the @ char as a key in a NSDictionary and my application simply crashes. I searched for “invalid” key names, could not find the @”@” anywhere. If I use something else than @”@” it all works fine.
I have a list of comanies, I get the first letter of each company and then I am creating a NSMutableDictionary entry containing the first letter as the key and a NSMutableArray as the value.
NSMutableDictionary indexDictionary = [[NSMutableDictionary alloc] init];
// here we have a loop on companyName
{
NSString *fristLetter = [[companyName substringToIndex:1] uppercaseString];
NSMutableArray *arrayOfIndexedCompanies = [indexDictionary valueForKey:firstLetter];
if (arrayOfIndexedCompanies) {
[arrayOfIndexedCompanies addObject:companyName]
}
else {
NSMutableArray *newArray = [NSMutableArray array];
[indexDictionary setObject:newArray forKey:firstLetter];
[newArray addObject:companyName];
}
}
I enabled breakpoint break on throw and it stops at the [indexDictionary valueForKey:firstLetter]… only when firstLetter is a @”@”.
I had an if saying:
if ([firstLetter isEqualToString:@"@"]) {
firstLetter = @"A";
}
and this works fine, it places the @ starting companies in the A section correctly. If I am letting the firstLetter unchanged (leaving it as @”@”) the application will crash.
Also, this is not really my code, I am just trying to fix it, I am not quite familiar with ObjC and Foundation so please be gentle.
Read the documentation 🙂
Specifically, this bit :
I guess that
super valueForKey:isn’t happy being called 🙂To fix it, just call
objectForKey:instead ofvalueForKey: