随机获取NSDictionary Key?

时间:2012-03-01 22:55:33

标签: iphone objective-c cocoa-touch cocoa nsdictionary

Hello StackOverflow社区,

<plist>
<dict>
    <key>Non Random Key</key>
    <dict>
        <key>Random Key</key>
        <dict>
            <key>Hello</key>
            <string>Hey</string>
            <key>Banana</key>
            <string>Bread</string>
        </dict>
    </dict>
</dict>
</plist>

这是我的Plist文件... /this/is/the/path.plist

现在我想从“Hello”中获取“Hey”字符串。

非随机密钥的解决方案! 但我有一个随机密钥,这也不是解决方案......

NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/this/is/the/path.plist"];

NSString *value;
value = [plistDict objectForKey:@"/Non Random Key/Random Key/Hello"];
// value is "Hey"

但它如何与“随机密钥”一起使用?

先谢谢你。

也许,您可以解释一下,我如何设置随机密钥? 顶部的示例也是“获取”我将“设置”:)

1 个答案:

答案 0 :(得分:1)

你可以迭代“非随机密钥”字典。这应该有所帮助:NSDictionary iterate

NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/this/is/the/path.plist"];

NSDictionary *nonRandomDic = [plistDic valueForKey:@"NonRandomKey"];


NSEnumerator *enumerator = [nonRandomDic keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
  NSString *tmp = [nonRandomDic objectForKey:key];
  NSLog(@"Your String %@ from the Random Key %@", tmp, key);
}