objective c - Best way to store iPhone data (custom NSObject) -
i have nsmutablearray
in store objects called "address". address nsobject
4-5 properties (nsstrings
). nsmutablearray
shall contain maximum of 15 address object.
what best way store array on iphone? core data
? nsuserdefaults
? should maybe store every address object itself, , not objects in 1 nsmutablearray
? in case should on iphone
?
thanks.
as @rog said, may use nsuserdefaults
save data
& should make object follow protocal nscoding
for examplem if object "youobject"
@interface youobject: nsobject { } @property (nonatomic, copy) nsstring *uid; @property (nonatomic, copy) nsstring *name; @end //implement 2 method - (id)initwithcoder:(nscoder *)decoder { if (self = [super init]) { self.title = [decoder decodeobjectforkey:@"uid"]; self.author = [decoder decodeobjectforkey:@"name"]; } return self; } - (void)encodewithcoder:(nscoder *)encoder { [encoder encodeobject:title forkey:@"uid"]; [encoder encodeobject:author forkey:@"name"]; }
then archive or unarchive using nsuserdefaults
//archive youobject *object = [youobject ....] nsdata *data = [nskeyedarchiver archiveddatawithrootobject:object ]; [[nsuserdefaults standarduserdefaults] setobject:data forkey:@"address"]; //unarchive nsdata *data = [[nsuserdefaults standarduserdefaults] objectforkey:@"address"]; youobject *object = (youobject *)[nskeyedunarchiver unarchiveobjectwithdata:data];
or if have youobject array, can save nsarray in same way;
//archive nsarray *addresses; nsdata *data = [nskeyedarchiver archiveddatawithrootobject:address ]; [[nsuserdefaults standarduserdefaults] setobject:data forkey:@"address"]; //unarchive nsdata *addressdata = [[nsuserdefaults standarduserdefaults] objectforkey:@"address"]; nsarray *addresses = (nsarray*)[nskeyedunarchiver unarchiveobjectwithdata:address];
Comments
Post a Comment