I have a dictionary which I am storing in another dictionary, which itself gets stored in an array, like so:
NSMutableDictionary* sectionHeights = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
[NSString stringWithFormat:@"%f", section1Height], @"Section 1 Height",
[NSString stringWithFormat:@"%f", section2Height], @"Section 2 Height",
nil];
[sectionObjects addObject:
[[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"Site Inspection Report", @"Form Name",
@"2", @"Section Count",
sectionHeights, @"Section Heights",
nil]
];
I pass the array to a property value of another class. When I try to read back sectionHeights, like so, the app crashes:
//get the subSectionData
NSDictionary* subSectionData = [sectionObjects objectAtIndex:sectionObjects.count-1];
NSDictionary* sectionHeights = [subSectionData objectForKey:@"Section Heights"];
for(id obj in sectionHeights) {
NSLog(@"%@", obj);
}
in this case sectionHeights has 2 objects, NSStrings which will get converted to floats, but looking at the console output it seems as if the app is trying to output 3 objects:
2012-11-01 10:59:26.338 OAI_Accordion[2402:c07] Section 1 Height
2012-11-01 10:59:26.339 OAI_Accordion[2402:c07] Section 2 Height
2012-11-01 10:59:26.340 OAI_Accordion[2402:c07] -[__NSCFConstantString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x9088
2012-11-01 10:59:26.341 OAI_Accordion[2402:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x9088'
*** First throw call stack:
(0x1c95012 0x10d2e7e 0x1d204bd 0x1c84bbc 0x1c8494e 0x46e8 0x2c84 0xfb817 0xfb882 0x4aa25 0x4adbf 0x4af55 0x53f67 0x225b 0x177b7 0x17da7 0x18fab 0x2a315 0x2b24b 0x1ccf8 0x1bf0df9 0x1bf0ad0 0x1c0abf5 0x1c0a962 0x1c3bbb6 0x1c3af44 0x1c3ae1b 0x187da 0x1a65c 0x1e9d 0x1dc5 0x1)
libc++abi.dylib: terminate called throwing an exception
I can’t see where I am setting it up wrong so I was hoping extra sets of eyes on it might help.
It looks like your application is crashing because of the loop:
Which is most likely why you’re getting the error
countByEnumeratingWithState:objects:count:as you’re trying to loop through a dictionary of values incorrectly.You could loop through your dictionary using enumeration via a block as such:
This gives you access to the dictionary keys and values side by side. Your previous implementation crashes as the compiler doesn’t know what to do with a dictionary in the context you have provided.
If you prefer the syntax of fast enumeration, you should loop through the array of dictionary values (or keys if you need them instead) by changing your code to:
You can also use [sectionHeights allKeys] if you need the dictionary keys instead.