ios - NSUserDefaults not being saved in simulator ,even though I use synchronize -
i add dictionary nsuserdefaults , call 'synchronize' method,but when close app , reopen values saved in nsuserdefault nil.
#define kstarttime @"starttime" #define kendtime @"endtime" nsuserdefaults *defaults=[nsuserdefaults standarduserdefaults]; nsstring *starttime=@"08:00"; nsstring *endtime=@"18:00"; nsdictionary *dict=[[nsdictionary alloc] initwithobjectsandkeys:starttime,kstarttime, endtime,kendtime, nil]; [defaults registerdefaults:dict]; [defaults synchronize];
and in viewdidload
method call this:
nsstring *starttimevalue=[defaults stringforkey:kstarttime];
the starttimevalue still nil when reopen app in simulator.is userdefaults can't save in simulator???
my point when close app , reopen in simulator values gone.
after searched apple document ,i got this.
registerdefaults:
.
as said in apple document :adds contents of specified dictionary registration domain.
*the contents of registration domain not written disk;*you need call method each time application starts.you can place plist file in application's resources directory , call registerdefaults: contents read in file.
so function of registerdefaults:
setting default values in app,the default value not stored in disk synchronize
i think try read value nsuserdefaults before have registered defaults. if register defaults these values not saved disk, not persist next application launch. have register default values every time launch app.
when read object nsuserdefaults , have not saved value before, registered default value returned. if have saved value before, value returned.
in code posted have not saved userdefaults. value have registered default returned. if haven't registered values return nil.
the code registers defaults should put in method runs in application life cycle. recommend use application:didfinishlaunchingwithoptions:
if want save multiple values nsuserdefaults have use multiple setobject:forkey:
calls.
registerdefaults:
can not used save multiple keys nsuserdefaults @ same time!
Comments
Post a Comment