2012-03-01 32 views
2

你好的StackOverflow社区,获取随机NSDictionary的关键?

<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”。

非随机密钥的解决方案! 但我有随机密钥,也该issn't解决方案...

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

你可以迭代“非随机密钥”字典。这应该有帮助: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); 
} 
+0

你的意思是我应该得到一个额外的Plist的“非随机密钥”Plist。 然后我用新的Plist创建一个“allKeys”数组。 – 2012-03-01 23:03:01

+0

我编辑了我的答案并添加了一个代码示例 – CarlJ 2012-03-01 23:08:00

+0

谢谢meccan, 是这样吗? 因为我想设置它,太:) http://pastebin.com/R6F02gPw 或与您的实例: http://pastebin.com/nPjzG4TY – 2012-03-01 23:14:54