iPhone:无法访问.plist文件中的所有值

时间:2011-03-21 10:31:40

标签: iphone plist nsuserdefaults

我有一个非常简单的root.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>StringsTable</key>
    <string>Root</string>
    <key>PreferenceSpecifiers</key>
    <array>
        <dict>
            <key>Type</key>
            <string>PSGroupSpecifier</string>
            <key>Title</key>
            <string>User Info</string>
        </dict>
        <dict>
            <key>Type</key>
            <string>PSTextFieldSpecifier</string>
            <key>Title</key>
            <string>Username</string>
            <key>Key</key>
            <string>username_preference</string>
            <key>DefaultValue</key>
            <string></string>
            <key>IsSecure</key>
            <false/>
            <key>KeyboardType</key>
            <string>Alphabet</string>
            <key>AutocapitalizationType</key>
            <string>None</string>
            <key>AutocorrectionType</key>
            <string>No</string>
        </dict>
        <dict>
            <key>Type</key>
            <string>PSTextFieldSpecifier</string>
            <key>Title</key>
            <string>Password</string>
            <key>Key</key>
            <string>password_preference</string>
            <key>DefaultValue</key>
            <string></string>
            <key>IsSecure</key>
            <true/>
            <key>KeyboardType</key>
            <string>Alphabet</string>
            <key>AutocapitalizationType</key>
            <string>None</string>
            <key>AutocorrectionType</key>
            <string>No</string>
        </dict>
        <dict>
            <key>Type</key>
            <string>PSGroupSpecifier</string>
            <key>Title</key>
            <string>Application Info</string>
        </dict>
        <dict>
            <key>Type</key>
            <string>PSTitleValueSpecifier</string>
            <key>Title</key>
            <string>Version</string>
            <key>Key</key>
            <string>api_version_preference</string>
            <key>DefaultValue</key>
            <string>2.0</string>
            <key>Value</key>
            <string>2.0</string>
        </dict>
    </array>
</dict>
</plist>

导航到iphone设置应用程序中的相关设置页面时会显示所有字段,但我无法从我的应用程序访问api_version_preference。

我试图将.plist文件打印为字典,我只看到用户名/密码字段中的值,所以我可能做错了。这是我在打印数据时使用的内容:

NSLog(@"defs: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

有人可以帮忙吗? :)

1 个答案:

答案 0 :(得分:1)

您忘记向NSUserDefaults提供默认值。如果您没有更改(即保存)它,NSUserDefaults不会存储该设置。

创建默认值的NSDictionary并使用[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsDefaults];

进行设置 像这样:

NSDictionary *userdefaultsDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"2.0", @"api_version_preference",
                                      nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:userdefaultsDefaults];

使用您在NSUserDefaults

中使用的所有设置执行此操作