Let's say that I define a ClassA , that implements NSCoding protocol (this class contains several variables such as NSString, NSArray etc.). I also create a ClassB, that has a variable of type ClassA in it. Something like this:
@interface ClassB: NSObject <NSCoding>
{
NSString *str;
NSDate *date;
int x;
ClassA *sc;
}
In my program, I'd like to archive a variable of type ClassB. Is it possible to recursiverly archive custom objects? If so, how?
Thanks!
You need to implement the NSCoding interface for your ClassB:
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self)
{
str = [[decoder decodeObjectForKey:@"MyStr"] retain];
date = [[decoder decodeObjectForKey:@"MyDate"] retain];
x = [decoder decodeIntegerForKey:@"MyX"];
sc = [decoder decodeObjectForKey:@"MyClassA"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[super encodeWithCoder:encoder];
[encoder encodeObject:str forKey:@"MyStr"];
[encoder encodeObject:date forKey:@"MyDate"];
[encoder encodeInteger:x forKey:@"MyX"];
[encoder encodeObject:sc forKey:@"MyClassA"];
}
ClassA must implement the NSCoding protocol as well.