NSMutableDictionary를 상속하고 NSKeyedArchiver로 직렬화 된 사용자 지정 개체는 역 직렬화 할 수 없습니다.

사샤 세웨로

수업이 제대로 보관 취소되지 않았습니다. 아래 코드 예제를 참조하십시오.

@interface A : NSMutableDictionary 
@end

@implementation A
- (void)encodeWithCoder:(NSCoder *)aCoder
{

}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {

    }   
    return self;
}
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    A *a = [[A alloc] init];

    NSMutableDictionary *dict = [NSMutableDictionary new];
    dict[@"some"] = a;

    NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:dict];

    dict = [NSKeyedUnarchiver unarchiveObjectWithData:archive];

    NSLog(@"%@",dict);
}

unarchiveObjectWithData 호출 후 dict에 @ "some"쌍 키가 포함되지만 개체는 A 클래스가 아닌 NSMutableDictionary입니다. unarchiveObjectWithData 호출 중에 initWithCoder 호출이 발생하지 않습니다. 그렇다면 코드는 어떻게 작동합니까? NSMutableDictionary를 상속하는 클래스가 역 직렬화되지 않는 이유는 무엇입니까?

이름

이 방법:

- (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

이번에는 정확히 테스트로 다시 편집하십시오 .

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);

로그 ...

{some = {hi = 거기; }; }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관