2012-03-01 33 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