Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

settings bundle - default value behaviour?

I have created a settings bundle with a single preference value, a text field with a default value.

When my application launches and I retrieve the value it is null. Do I have to manually code the value to use before the user has provided a value?

Also if the user does to the preferences screen and enters the text field then leave it without making any changes the value will not be set ... the user has to actually change the value for it to be saved, is this correct?

like image 652
Nippysaurus Avatar asked Apr 12 '11 06:04

Nippysaurus


2 Answers

No, don't do that.

There's a function for this:

id userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults registerDefaults: defaultSettings];

You can build the parameter by iterating all the sub keys of PreferenceSpecifiers in your settings plist file.

Something like this:

- (NSDictionary *)readDefaultSettingsFromPlist: (NSString *)inPath;
{
    id mutable = [NSMutableDictionary dictionary];
    id settings = [NSDictionary dictionaryWithContentsOfFile: inPath];
    id specifiers = [settings objectForKey: @"PreferenceSpecifiers"];
    for (id prefItem in specifiers) {
        id key = [prefItem objectForKey: @"Key"];
        id value = [prefItem objectForKey: @"DefaultValue"];
        if ( key && value ) {
            [mutable setObject: value
                        forKey: key];
        }
    }
    return [NSDictionary dictionaryWithDictionary: mutable];
}
like image 169
Steven Fisher Avatar answered Sep 30 '22 19:09

Steven Fisher


You can check in your app if you are getting null for key then you can use your own default value.

like image 44
saadnib Avatar answered Sep 30 '22 20:09

saadnib