Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

iphone - Multivalue type settings bundle fields alway return 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"];

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>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...