Lets say we have an array sampleArray which stores some objects of class called "MyClass"
sampleArray=[[NSMutableArray alloc]init];
Now you want to save this array called sampleArray in NSUserDefault. You will have to use following code.
As you can see, you will have to use NSKeyedArchiver. you can't use straightforward setObject here otherwise you will get warning like:
Later on, when you want to fetch your array from NSUserDefault, you can write following code. Remember it will return non-mutable array.
Well. Last thing, don't forget to add following encode and decode method in your MyClass.
where MyClass has declaration as following
I have created a sample code for those who still need some clarification. Hope this helps.
Download the Sample Code.
sampleArray=[[NSMutableArray alloc]init];
MyClass *obj1=[[MyClass alloc]init];
obj1.name=@"Reetu";
obj1.sex=@"F";
obj1.income=@"12";
obj1.city=@"GUN";
obj1.mothername=@"ReetuSr.";
obj1.ethnicity=@"Asian";
obj1.fathername=@"ReetuDad";
obj1.countOpen=1;
[sampleArray addObject:obj1];
MyClass *obj2=[[MyClass alloc]init];
obj2.name=@"Pinku";
obj2.sex=@"M";
obj2.income=@"1223";
obj2.city=@"ASC";
obj2.mothername=@"PinkuSr.";
obj2.ethnicity=@"American";
obj2.fathername=@"PinkuDad";
obj2.countOpen=2;
[sampleArray addObject:obj2];
MyClass *obj3=[[MyClass alloc]init];
obj3.name=@"Mike";
obj3.sex=@"M";
obj3.income=@"125";
obj3.city=@"PENN";
obj3.mothername=@"Fred.";
obj3.ethnicity=@"Caucasian";
obj3.fathername=@"Fred";
obj3.countOpen=6;
[sampleArray addObject:obj3];Now you want to save this array called sampleArray in NSUserDefault. You will have to use following code.
NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:sampleArray];
[userDefault setObject:myEncodedObject forKey:[NSString stringWithFormat:@"sample"]];
As you can see, you will have to use NSKeyedArchiver. you can't use straightforward setObject here otherwise you will get warning like:
-[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '' of class 'MyClass'.
Later on, when you want to fetch your array from NSUserDefault, you can write following code. Remember it will return non-mutable array.
//Lets decode it now
NSData *myDecodedObject = [userDefault objectForKey: [NSString stringWithFormat:@"sample"]];
NSArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject];
//Print the array received from User's Default
for (MyClass *item in decodedArray) {
NSLog(@"name=%@",item.name);
NSLog(@"sex=%@",item.sex);
NSLog(@"income=%@",item.income);
NSLog(@"city=%@",item.city);
NSLog(@"-----------");
}
Well. Last thing, don't forget to add following encode and decode method in your MyClass.
- (void)encodeWithCoder:(NSCoder *)encoder
{
//Encode properties, other class variables, etc
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeObject:self.sex forKey:@"sex"];
[encoder encodeObject:self.income forKey:@"income"];
[encoder encodeObject:self.city forKey:@"city"];
[encoder encodeObject:self.mothername forKey:@"mothername"];
[encoder encodeObject:self.ethnicity forKey:@"ethnicity"];
[encoder encodeObject:self.fathername forKey:@"fathername"];
[encoder encodeObject:[NSNumber numberWithInt:self.countOpen] forKey:@"destinationCode"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if( self != nil )
{
self.name = [decoder decodeObjectForKey:@"name"];
self.sex = [decoder decodeObjectForKey:@"sex"];
self.income = [decoder decodeObjectForKey:@"income"];
self.city = [decoder decodeObjectForKey:@"city"];
self.mothername = [decoder decodeObjectForKey:@"mothername"];
self.ethnicity = [decoder decodeObjectForKey:@"ethnicity"];
self.fathername = [decoder decodeObjectForKey:@"fathername"];
self.countOpen = [[decoder decodeObjectForKey:@"countOpen"] intValue];
}
return self;
}
where MyClass has declaration as following
@interface MyClass : NSObject {
NSString *name;
NSString *sex;
NSString *income;
NSString *city;
NSString *mothername;
NSString *ethnicity;
NSString *fathername;
int countOpen;
}I have created a sample code for those who still need some clarification. Hope this helps.
Download the Sample Code.
Thank you so much for this. It was a great help to me!
ReplyDeleteVery good tutorial. But I have one question, how to get NSMutableArray like variable in Class???
ReplyDeletethank u so much it helped me a lot :)
ReplyDeletePerfect Article solved my problem and made me clear
ReplyDelete