views:

33

answers:

1

Hello,

Can i decode and encode an array in ths way??

NSMutableArray *arr;

-(void)encodeWithCoder:(NSCoder*)coder 
{ 
   [coder encodeInt:arr forKey:@"Array"]; 
} 

-(id)initWithCoder:(NSCoder*)decoder 
{ 
   NSMutableArray array;
   array = [[decoder decodeIntForKey:@"Array"]copy]; 
   return self; 
} 
A: 

It will not encode/decode the NSMutableArray that way.

If objects in your array implement NSCoding protocol then you can encode array by calling:

NSMutableArray * myMutableArray = ...
NSData* myData = [NSKeyedArchiver archivedDataWithRootObject:myMutableArray];

And reverse with:

NSMutableArray* myMutableArray = [NSKeyedUnarchiver
                                             unarchiveObjectWithData:myData];
stefanB