I’m using the following code to browse a NSMutableDictionary
NSMutableDictionary *item;
for (item in menuArray) {
BSThirdLayerViewController *nextLayer = [[BSThirdLayerViewController alloc] initWithStyle:UITableViewStylePlain];
nextLayer.title = [item valueForKey:@"title"];
nextLayer.rowImage = [item valueForKey:@"image"];
[array addObject:nextLayer];
}
The dictionary is loaded from the following plist file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Item 0</key>
<dict>
<key>title</key>
<string>East Coast</string>
</dict>
<key>Item 1</key>
<dict>
<key>title</key>
<string>West Coast</string>
</dict>
<key>Item 2</key>
<dict>
<key>title</key>
<string>South Rhodes</string>
</dict>
</dict>
</plist>
What kind of data will the item variable have during the for-in loop?
The program crashes when trying to access
[item valueForKey:@"title"];
It seems logical, but what’s going on?
Thank you for your time.
The plist appears to define a dictionary, not an array. Thus “item” will be a key (i.e. a string), Item 0, Item 1, etc. With that key, you can then ask the dictionary for the value of that key, using NSDictionary *dict = [dictionary objectForKey:item];
When you have this kind of problem, let the system help you – see what’s in the object you created from the plist by using NSLog() to dump it to the console. It will save you a lot of time and confirm that in fact you are getting what you wanted.