【问题标题】:Navigating a Dictionary of Arrays of Dictionaries导航字典数组的字典
【发布时间】:2013-09-04 21:43:05
【问题描述】:

我已经为此苦苦挣扎了一个多月,所以我试图自己解决这个问题。

我有一个包含项目字典的列表 plist。例如,如何下钻以显示购物清单中商品的选中状态?

<dict>
<key>Shopping List</key>
<array>
    <dict>
        <key>Name</key>
        <string>Green Tea</string>
        <key>Checked</key>
        <false/>
    </dict>
    <dict>
        <key>Name</key>
        <string>Tempeh</string>
        <key>Checked</key>
        <false/>
    </dict>
    <dict>
        <key>Name</key>
        <string>Kimuchi</string>
        <key>Checked</key>
        <false/>
    </dict>
    <dict>
        <key>Name</key>
        <string>Jasmine Rice</string>
        <key>Checked</key>
        <false/>
    </dict>
</array>
<key>To-Do List</key>
<array>
    <dict>
        <key>Name</key>
        <string>Wash Car</string>
        <key>Checked</key>
        <false/>
    </dict>
    <dict>
        <key>Name</key>
        <string>Run Diswasher</string>
        <key>Checked</key>
        <false/>
    </dict>
</array>

【问题讨论】:

  • 发布您迄今为止与访问此 plist 中的数据相关的相关代码。然后有人可以帮助您修复它。

标签: objective-c arrays dictionary


【解决方案1】:

试试这个:

// First we get the path for the plist
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"PlistFileName" ofType:@"plist"]; // Replace PlistFileName with your plist name (without its file extension)

// Then, we get the first dictionary (containing "Shopping List" and "To-Do List") 
NSDictionary *listsDictionary = [[NSDictionary alloc] initWithContentsOfFile:thePath];

// Then, we get the array contained in the "Shopping List" object of the previous dictionary we got
NSArray *theShoppingList = [listsDictionary objectForKey:@"Shopping List"];

// Then, we get the dictionary representing an item of the list at a given index in the previous array we got
NSDictionary *item = (NSDictionary *)[theShoppingList objectAtIndex:index]; // index represents the index of the item in the plist

// Last but not least, we get the Checked value (as a boolean value) contained in the item
Boolean checked = [[item objectForKey:@"Checked"] boolValue];

【讨论】:

  • 非常感谢您的(评论)回答,我真的很感激。一个问题,如果我事先不知道“给定索引”,我怎样才能通过 listsDictionary 快速枚举来获取索引?
  • 您的意思是枚举“theShoppingList”数组,不是吗?枚举字典并不能保证排序...
  • 你刚刚强调了我还有多少需要学习。是的,我注意到字典不保证排序。改写我的问题:如果我事先不知道“给定索引”,我将如何获取 plist 中项目的索引
  • 这取决于你想如何确定它,你可以通过“theShoppingList”用“for in”这样的语句来枚举,它可以让你确定你想要的项目,但可能不是最快的解决方案(我也在学习,所以我不能给你更好的解决这个问题...):for (NSDictionary *item in theShoppingList) { NSString *name = [[item objectForKey:@"Name"] stringValue]; // Check if this is the item you want ? }
  • 再次感谢,我会试试的。
【解决方案2】:

当你将文件读入 NSDictionary 时,你会有这样的树形结构,

@{ 
  @"Shopping List" :
   @[
     @{
        @"Name": @"Green Tea",
        @"Checked": [NSNumber numberWithBool: NO]
    },
    @{
        @"Name": @"Tempeh", 
        @"Checked": [NSNumber numberWithBool: NO]
    },
    @{
        @"Name": @"Jasmine Rice", 
        @"Checked": [NSNumber numberWithBool: NO]
    },
  ],

  @"To-Do List":
  @[
    @{
        @"Name": @"Wash Car", 
        @"Checked": [NSNumber numberWithBool: NO]
    },
    @{
        @"Name": @"Run Diswasher", 
        @"Checked": [NSNumber numberWithBool: NO]
    },
  ]
}

我已经使用新的 Objective C 字典文字从上面的 plist 创建了字典。这应该更有意义,并且可以很容易地解析。

所以,如果“绿茶”被选中,你会这样做,

dict[@"Shopping List"][0][@"Checked"]

对于茉莉香米,

dict[@"Shopping List"][2][@"Checked"]

洗车,

dict[@"To-Do list"][0][@"Checked"]

我创建了一个小帮助函数,您可以使用它枚举所有键并为某些键选择特定值,

- (void)enumerateDictionary:(NSDictionary*)dictionary{
  for(NSString *key in dictionary){
    id object = [dictionary valueForKey:key];
    if([object isKindOfClass:[NSArray class]])
      for(id dict in object)
        [self enumerateDictionary:dict];
    else if([object isKindOfClass:[NSDictionary class]])
        [self enumerateDictionary:object];
    else if([object isKindOfClass:[NSString class]])
        NSLog(@"Name => %@\nChecked => %@\n", dictionary[@"Name"], dictionary[@"Checked"]);
  }
}

当您阅读字典时,只需将字典传递给此函数,它将显示所有值和相应的检查值。然后,您可以添加一些代码来选择特定的代码。

【讨论】:

  • 谢谢,这也很有帮助。致你们俩:对不起,我不能投票给你们的答案。
  • 你是什么意思,你为什么不能投票给答案。你的问题到底是什么?你想做什么?
  • 投票需要 15 的声望。这是我的第一篇文章
  • 没问题。很高兴我能帮上忙 :)。这些我都经历过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多