ios - how can i store parse object directly in NsuserDefault? -


pfquery *location = [pfquery querywithclassname:@"location"]; [location findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error) {         nslog(@"%@", [objects objectatindex:0]);              }]; 

how can store object in nsuserdefaults?

for storing custom objects u need add these 2 methods in m file of custom object class

-(void)encodewithcoder:(nscoder *)encoder {     //encode properties of object     [encoder encodeobject:self.contact_fname forkey:@"contact_fname"];     [encoder encodeobject:self.contact_lname forkey:@"contact_lname"];     [encoder encodeobject:self.contact_image forkey:@"contact_image"];     [encoder encodeobject:self.contact_phone_number forkey:@"contact_phone_number"];  }  -(id)initwithcoder:(nscoder *)decoder {     self = [super init];     if ( self != nil )     {         //decode properties         self.contact_fname = [decoder decodeobjectforkey:@"contact_fname"];         self.contact_lname = [decoder decodeobjectforkey:@"contact_lname"];         self.contact_image = [decoder decodeobjectforkey:@"contact_image"];         self.contact_phone_number = [decoder decodeobjectforkey:@"contact_phone_number"];     }     return self; } 

then

-(void)writearraywithcustomobjtouserdefaults:(nsstring *)keyname witharray:(nsmutablearray *)myarray {     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];     nsdata *data = [nskeyedarchiver archiveddatawithrootobject:myarray];     [defaults setobject:data forkey:keyname];     [defaults synchronize]; }  -(nsarray *)readarraywithcustomobjfromuserdefaults:(nsstring*)keyname {     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];     nsdata *data = [defaults objectforkey:keyname];     nsarray *myarray = [nskeyedunarchiver unarchiveobjectwithdata:data];     [defaults synchronize];     return myarray; } 

use these functions storing , reading custom object arrays or u use library https://github.com/roomorama/rmmapper


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -