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];
精彩评论