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

时间:2012-07-17 23:05:22

标签: objective-c arrays dictionary nsmutabledictionary

我有以下PLIST,需要访问嵌套在字典中的助手标识符。问题是Accounts下的第一个键(以4FD9E669 ...开头)对每个设备都是唯一的。如何在不知道字典键的情况下返回助理标识符?

这是我的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];

请帮忙!谢谢!

1 个答案:

答案 0 :(得分:1)

乍一看,您可以循环浏览所有按键,直到找到所需的按键。

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.
    }
}

希望有所帮助。