Initialization of NSDictionary results in "EXC_BAD_ACCESS"

I cannot understand why this code is bad. I thought using static lines in the dictionaryWithObjectsAndKeys, but this fails at runtime with EXC_BAD_ACCESS. I used a debugger to determine that it does not actually work in the definition line. He never displays "Did 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 } 
+4
source share
2 answers

The problem is here:

 @"pentagon","5" 

It expects an object (NSString), but a regular string exists instead.
Change it to:

 @"pentagon", @"5" 
+18
source

What line is EXC_BAD_ACCESS in? Use breakpoints and let us know.

For now, if I were you, I would do something like this:

 NSDictionary *polNameDict = [[NSDictionary alloc] initWithObjectsAndKeys: @"triangle", @"3", @"square", @"4", @"pentagon",@"5", @"Hexagon", @"6", @"Heptagon", @"7", @"Octagon", @"8", @"Nonagon", @"9", @"Decagon", @"10", @"Hendecagon", @"11", @"Dodecagon", @"12", nil]; 

not like him now.

-4
source

All Articles