从未知字典键名读取iOS Plist值?

问题描述:

我有以下PLIST,需要访问嵌套在字典中的助手标识符。问题是帐户下的第一个密钥(以4FD9E669开头)对每个设备都是唯一的。如何在不知道字典密钥的情况下返回助手标识符?从未知字典键名读取iOS Plist值?

这里是我的PLIST:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>Accounts</key> 
    <dict> 
     <key>4FD9E669-A5EA-4526-A6D2-0440858221A6</key> 
     <dict> 
      <key>Assistant Identifier</key> 
      <string>05483ADC-23E1-400E-83E8-F7365063F56F</string> 
      <key>Last Assistant Data Anchor</key> 
      <string>c1e3ad3c51d7fb962b29ca2deb4990c0f6ae8962</string> 
      <key>Speech Identifier</key> 
      <string>dcaf7c87-c023-456c-a200-7db8bd5e10f1</string> 
      <key>Validation Expiration</key> 
       <date>2012-07-17T22:37:37Z</date> 
     </dict> 
    </dict> 
    <key>Session Language</key> 
    <string>en-US</string> 
</dict> 
</plist> 

我都试过,如果A.Identifier是已知的,其工作原理如下...(这不利于)

NSString* filename = @"/var/mobile/Library/Preferences/com.apple.assistant.plist"; 
NSMutableDictionary* prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:filename]; 

NSMutableDictionary* nestedPrefs = (NSMutableDictionary*)[prefs valueForKey:@"Accounts"]; 

NSMutableDictionary* aPrefs = (NSMutableDictionary*)[nestedPrefs valueForKey:@"4FD9E669-A5EA-4526-A6D2-0440858221A6"]; 

NSString* assistantPref = (NSString*)[aPrefs valueForKey:@"Assistant Identifier"]; 

UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Assistant Identifier" message:assistantPref delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
[theAlert show]; 
[theAlert release]; 

请帮帮忙!谢谢!

乍一看,你可以循环所有的钥匙,直到你找到你想要的。

for (NSString *key in [nestedPrefs allKeys]) { 
    // Pick which key is the one you want 
} 

我不认为你需要那样做。这是逻辑,只是跳过找到正确的钥匙的步骤。

for (NSMutableDictionary *aPrefs in [nestedPrefs allValues]) { 
    NSString* assistantPref = [aPrefs valueForKey:@"Assistant Identifier"]; 
    if (assistantPref) { 
     // Whatever you need. 
    } 
} 

希望有所帮助。