多值类型设置捆绑字段总是返回null [英] Multivalue type settings bundle fields alway return null

查看:113
本文介绍了多值类型设置捆绑字段总是返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次启动应用时,我在设置包中检索默认值时遇到问题。所有非多值字段都会正确返回默认值。所有多值字段都返回NULL。我用来检索当前值的代码:

I have a problem retrieving default values from the settings bundle when first launching an App. All non-multivalue fields return the default correctly. All multivalue fields return NULL. The code I am using to retrieve the current value:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *upt=[defaults objectForKey:@"upthreshold"];

Root.plist的相关部分是:

The relevant section from Root.plist is:

                <dict>
                    <key>Title</key>
                    <string>Update Threshold</string>
                    <key>Type</key>
                    <string>PSMultiValueSpecifier</string>
                    <key>Key</key>
                    <string>upthreshold</string>
                    <key>DefaultValue</key>
                    <string>15</string>
                    <key>Titles</key>
                    <array>
                            <string>2 km</string>
                            <string>5 km</string>
                            <string>10 km</string>
                            <string>15 km</string>
                            <string>25 km</string>
                            <string>50 km</string>
                            <string>75 km</string>
                            <string>100 km</string>
                            <string>150 km</string>
                            <string>250 km</string>
                            <string>500 km</string>
                            <string>1000 km</string>
                    </array>
                    <key>Values</key>
                    <array>
                            <string>2</string>
                            <string>5</string>
                            <string>10</string>
                            <string>15</string>
                            <string>25</string>
                            <string>50</string>
                            <string>75</string>
                            <string>100</string>
                            <string>150</string>
                            <string>250</string>
                            <string>500</string>
                            <string>1000</string>
                    </array>
            </dict>

我希望15会作为当前值返回,因为它设置为默认值值。 Multivalue字段是否需要一些不同的处理?正如我之前所说,在此之前有6个字段可以正确返回它们的值。此字段和以下2个多值字段不会。多值字段都定义相同。

I would expect that "15" would come back as the current value as it is set to the default value. Is there some different processing required for Multivalue fields? As I said previously, there are 6 fields prior to this that return their values correctly. This field and the following 2 Multivalue fields do not. The multivalue fields are all defined the same.

推荐答案

如果用户未在设置应用中更改该值,则没有设置。
设置包中指定的默认值仅为设置应用程序中显示的默认值

If the value wasn't changed by the user in the settings app there is no setting. The default value specified in the settings bundle is only the default for display in the settings app

您必须手动注册默认值。 NSUserdefaults不使用设置包中的默认值。

使用类似的东西,在应用程序的开头添加它(在访问用户默认设置之前)。

注册的用户默认值是没有保存到磁盘或任何东西。每次启动应用程序时都必须注册它们。

you have to manually register your default values. NSUserdefaults doesn't use the defaults from the settings bundle.
Use something like this, add it at the beginning of your app (before you access the userdefaults).
Registered Userdefaults are not saved to disk or anything. You have to register them every time you start the app.

NSDictionary *userDefaultsDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"15", @"upthreshold",
                                      nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsDefaults];

这篇关于多值类型设置捆绑字段总是返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆