Dictionary within a dictionary (as a plist)
I have a plist that is set up like:
As you can see, I have a dictionary within a dictionary: each parent node has children nodes, which themselves are a dictionaryy with images and badges associated with them.
The part I'm stuck at is how to turn this into a datasource for a NSOutlineView..
At the moment I have this:
NSString *paths = [[NSBundle mainBundle] pathForResource:@"navigationItems" ofType:@"plist"];
navigationItems = [[NSMutableDictionary alloc] initWithContentsOfFile:paths];
NSArray *parents = [navigationItems allKeys开发者_运维技巧];
for (NSString *parent in parents) {
SourceListItem *parentItem = [SourceListItem itemWithTitle:parent identifier:parent];
NSArray *children = [parent allKeys];
NSLog(@"%@", children);
}
I was going to then do a for loop with children, but the problem is is that parent is a NSString and you can't for loop over a NSString..
So what should I do?
Many thanks
If I'm reading your code correctly, parent
is a key for a dictionary containing child dictionaries in your plist. You need to grab the dictionary that parent
refers to in navigationItems
before accessing the embedded child dictionary data. For example:
NSString *paths = [[NSBundle mainBundle] pathForResource:@"navigationItems" ofType:@"plist"];
navigationItems = [[NSMutableDictionary alloc] initWithContentsOfFile:paths];
NSArray *parents = [navigationItems allKeys];
for (NSString *parent in parents) {
SourceListItem *parentItem = [SourceListItem itemWithTitle:parent identifier:parent];
NSDictionary *parentData = [navigationItems objectForKey:parent];
NSArray *children = [parentData allKeys];
NSLog(@"%@", children);
}
Remember to access the dictionary data for each of the children in the same manner when you loop through their keys.
精彩评论