4

カスタムクラスのインスタンスが設定されたmainObjectArray(NSMutableArray)があります。各インスタンスはそれ自体が配列であり、各配列のオブジェクトはNSDates、NSStrings、BOOL、および同様のオブジェクトを含むその他の配列です。

私が確立できなかったのは、内部でそれが可能かどうかです

- (void)encodeWithCoder:(NSCoder *)encoder 

メソッド、そのようなことを言うには:

[encoder encodeWithObject:mainObjectArray];

または、すべてのインスタンスのすべてのオブジェクトを個別にエンコードする必要がありますか?これは少し苦痛でしょう...

あなたの助けをいただければ幸いです。

4

1 に答える 1

6

カスタムクラスにエンコードメソッドとデコードメソッドを実装するだけです。それで十分です。いくつかのサンプル、

- (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"];
    }
}

エンコードおよびデコードされているデータ型は、int、string、arrayの3つです。

お役に立てれば。

于 2012-02-18T18:03:33.297 に答える