【问题标题】:Reading iOS Plist value from unknown dictionary key name?从未知的字典键名中读取 iOS Plist 值?
【发布时间】:2012-07-17 23:05:21
【问题描述】:

我有以下 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];

请帮忙!谢谢!

【问题讨论】:

    标签: objective-c arrays dictionary nsmutabledictionary


    【解决方案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.
        }
    }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-02-21
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      相关资源
      最近更新 更多