字典中的字典(作为 plist)

发布于 2024-11-26 14:36:47 字数 761 浏览 1 评论 0原文

我有一个设置如下的 plist:

在此处输入图像描述

正如你所看到的,我在字典中有一本字典:每个父节点都有子节点,子节点本身就是一个字典,其中包含与其关联的图像和徽章。

我陷入困境的部分是如何将其转换为 NSOutlineView 的数据源。

目前我有这个:

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);
}

我打算对子级进行 for 循环,但问题是父级是 NSString 并且你不能 for 循环 NSString ..

那我该怎么办?

非常感谢

I have a plist that is set up like:

enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惯饮孤独 2024-12-03 14:36:47

如果我正确地阅读了您的代码,则 parent 是您的 plist 中包含子字典的字典的键。在访问嵌入的子字典数据之前,您需要获取 parentnavigationItems 中引用的字典。例如:

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);
}

当您循环访问每个子项的键时,请记住以相同的方式访问每个子项的字典数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文