I can not figure out why this code is bad. I thought it was ok to use static strings in dictionaryWithObjectsAndKeys however it fails at runtime with a EXC_BAD_ACCESS. I used the debugger to identify that it is in fact failing at the definition line. It never outputs “Made it here”.
- (NSString *)polyName:(NSUInteger)vertices {
NSLog(@"looking for polyName with %d vertices.", vertices);
NSDictionary *polNameDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"triangle", @"3",
@"square", @"4",
@"pentagon","5",
@"Hexagon", @"6",
@"Heptagon", @"7",
@"Octagon", @"8",
@"Nonagon", @"9",
@"Decagon", @"10",
@"Hendecagon", @"11",
@"Dodecagon", @"12",
nil];
NSLog(@"Made it here");
NSString *key = [NSString stringWithFormat: @"%d", vertices];
NSString *polName = [polNameDict objectForKey:key];
return (polName);
// Memory management: No need to release polNameDict, key, polName because they use
// convenience functions
}
The problem is here:
It expects an object (NSString) but instead there’s a regular string.
Change it to: