質問

I have a mainObjectArray (NSMutableArray) which is populated with instances of a custom class. Each instance is itself an array, and objects in each array are NSDates, NSStrings, BOOL, and more arrays containing similar objects.

What I haven't been able to establish is whether it's possible to, inside the

- (void)encodeWithCoder:(NSCoder *)encoder 

method, to just say something like that:

[encoder encodeWithObject:mainObjectArray];

Or do have to encode every object in every instance separately? This would be a bit of a pain...

Your help would be very much appreciated.

役に立ちましたか?

解決

Just implement the encoding and decoding methods in your custom class. That will do. Some sample,

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:[NSNumber numberWithInt:pageNumber] forKey:@"pageNumber"];
    [encoder encodeObject:path forKey:@"path"];
    [encoder encodeObject:array forKey:@"array"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init]) 
    {
        self.pageNumber = [[aDecoder decodeObjectForKey:@"pageNumber"] intValue];
        self.path = [aDecoder decodeObjectForKey:@"path"];
        self.array = [aDecoder decodeObjectForKey:@"array"];
    }
}

You can see totally three data types being encoded and decoded - int, string, array.

Hope this helps.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top