cocoa - How to register user defaults using NSUserDefaults without overwriting existing values? -
i have appdelegate class +(void)initialize
method use register defaults. here's code use:
+ (void)initialize { nsdictionary *defaults = [nsdictionary dictionarywithobjectsandkeys:@"no", @"fookey", @"yes", @"barkey", nil]; [[nsuserdefaults standarduserdefaults] registerdefaults:defaults]; }
i created preferences.xib holds couple of checkboxes (nsbutton
) display status of preferences. bound nsuserdefaultscontroller
same keys (fookey , barkey in case). each time launch app , change "defaults" restored on next app launch.
is there way register "default defaults" without overwriting existing values? maybe each time build , launch app preferences file being recreated? maybe should unbind checkboxes nsuserdefaultscontroller
, maintain values of keys myself custom code in preferences window controller?
i'd hear implementation of choice maintaining user defaults.
i'm using mac os x 10.6.2 , xcode 3.2.1
from documentation -registerdefaults:
(emphasis added):
the contents of registration domain not written disk; you need call method each time application starts. can place plist file in application's resources directory , call registerdefaults: contents read in file.
so code on right track. how register default defaults.
i use in -applicationdidfinishlaunching:
:
// load default defaults [[nsuserdefaults standarduserdefaults] registerdefaults:[nsdictionary dictionarywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"defaults" oftype:@"plist"]]];
using plist makes easy add , change defaults in app, , prevents making mistake of using @"no"
value too.
edit: swift 3 variant:
userdefaults.standard.register(defaults: nsdictionary(contentsof: bundle.main.url(forresource: "defaults", withextension: "plist")!)! as! [string : any])
Comments
Post a Comment