开发者

decoding and encoding array in Objective-C

开发者 https://www.devze.com 2023-01-11 17:04 出处:网络
Can i decode and encode an array in ths way?? NSMutableArray *arr; -(void)encodeWithCoder:(NSCod开发者_运维问答er*)coder

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

NSMutableArray *arr;

-(void)encodeWithCoder:(NSCod开发者_运维问答er*)coder 
{ 
   [coder encodeInt:arr forKey:@"Array"]; 
} 

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


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消