ios - Store Array into NSUserDefaults -
i've got small problem permanently storing array using nsuserdefaults.
extract of viewcontroller.m
@property (strong, nonatomic) nsmutablearray *locationarray; - (ibaction)onaddclick:(id)sender { nslog(@"onaddclick"); cllocationdegrees lat = self.mapview.userlocation.location.coordinate.latitude; cllocationdegrees longi = self.mapview.userlocation.location.coordinate.longitude; cllocation *currentlocation = [[cllocation alloc] initwithlatitude:lat longitude:longi]; [self.locationarray addobject:currentlocation]; nsdata *data = [nskeyedarchiver archiveddatawithrootobject:self.locationarray]; [[nsuserdefaults standarduserdefaults] setobject:data forkey:@"locationdata"]; [[nsuserdefaults standarduserdefaults] synchronize]; }
and in locationtableviewcontroller.m want retrieve array:
nsdata* data = [[nsuserdefaults standarduserdefaults] objectforkey:@"locationdata"]; self.locationarray = [nskeyedunarchiver unarchiveobjectwithdata:data]; if (data != nil) { nslog(@"found data"); } if (self.locationarray.count > 0) { nslog(@"found array"); }
when dont close app, works fine , can retrieve data userdefaults.. after closing, data doesn't exist anymore... thought nsuserdefaults designed store data permanently? tips?
i think problem archived array object contains cllocation
object, nsuserdefault
save types of objects nsdata, nsnumber, nsstring, nsarray
, , object contained in nsarray should 1 of these types.
so archiveddatawithrootobject
each cllocation
object nsarray
, means nsarray
contains nsdata
in , try again.
Comments
Post a Comment