この方法:
- (void)encodeWithCoder:(NSCoder *)aCoder
{
}
は「何もしない」、と言うことである、自分自身をエンコードするために、オブジェクトのための説明書が含まれています。何それは言うべきことは次のとおりです。
- (void)encodeWithCoder:(NSCoder *)aCoder
{
// perform the inherited behavior or encoding myself
[super encodeWithCoder:encoder];
}
EDIT再び、このテストクラスは、ほとんどの些細な方法でNSMutableDictionary
をサブクラス:それには可変辞書インスタンスを非表示にすることで、実装と原始的な方法(PLUS encodeWithCoder
)を提供します
#import "MyDict.h"
@interface MyDict()
@property(strong) NSMutableDictionary *internalDictionary;
@end
@implementation MyDict
// changed to the default init for the "new" constructor
- (id)init {
self = [super init];
if (self) {
_internalDictionary = [[NSMutableDictionary alloc] init];
}
return self;
}
- (NSUInteger)count {
return [self.internalDictionary count];
}
- (id)objectForKey:(id)aKey {
return [self.internalDictionary objectForKey:aKey];
}
- (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
return [self.internalDictionary setObject:anObject forKey:aKey];
}
- (void)removeObjectForKey:(id)aKey {
return [self.internalDictionary removeObjectForKey:aKey];
}
- (NSEnumerator *)keyEnumerator {
return [self.internalDictionary keyEnumerator];
}
// added encoding of the internal representation
- (void)encodeWithCoder:(NSCoder *)aCoder {
[super encodeWithCoder:aCoder];
[aCoder encodeObject:_internalDictionary forKey:@"internalDictionary"];
}
// added decoding of the internal representation
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
_internalDictionary = [aDecoder decodeObjectForKey:@"internalDictionary"];
}
return self;
}
- (Class)classForCoder {
return self.class;
}
@end
EDIT再び、正確にあなたのテストでこの時間:
MyDict *a = [MyDict new];
a[@"hi"] = @"there";
NSMutableDictionary *dict = [NSMutableDictionary new];
dict[@"some"] = a;
NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:dict];
dict = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
NSLog(@"%@", dict);
の
ログ...
{ 一部= { HI =そこ。 }; }
申し訳ありませんが、動作しません。 まず、これをチェックしましたか? 私は試してクラッシュしました。 –
クラッシュは何と言いますか?すべてのNSMutableDictionaryプリミティブメソッドを実装しましたか?また、不変のディクショナリプリミティブメソッドのすべてを実装する必要があることを覚えておいてください。 – danh
@SashaSewerow - 正当性を示すコードが追加されました。 – danh