开发者

decoding and encoding array in Objective-C

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜