Невозможно десериализовать заархивированный объект с помощью NSKeyedUnarchiver - PullRequest
0 голосов
/ 23 сентября 2019

Я сериализую пользовательский объект, используя NSKeyedArchiver.

@interface DLTree : NSObject<NSSecureCoding>
@property (nonatomic, readwrite, retain) NSString *name;
@property (nonatomic, readwrite, retain) id value;
@property (nonatomic, readwrite, retain) NSDecimalNumber *weight;
@property (nonatomic, readwrite, retain) NSMutableArray *children;  // contains DLTree objects
@property (nonatomic, readwrite, retain) NSData *sortHint;

Я архивирую его, используя приведенный ниже код, и сохраняю на диск.Plist при декодировании в xml показывает, что он правильно сформирован и не пустой.

NSMutableDictionary *dict = [NSMutableDictionary new];
[dict setObject:state forKey:@"state"];  // The state object with the tree I have created
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dict requiringSecureCoding:YES error:&err];

Для декодирования я использую:

NSSet *set = [[NSSet alloc] initWithArray:@[[NSMutableDictionary class], [DLState class], [DLTree class], [NSValue class], [NSMutableArray class], [NSString class], [NSDecimalNumber class]]];

Класс DLState:

@interface DLState: NSObject<NSSecureCoding>
@property (nonatomic, readwrite, retain) DLTree *tree;
@end

Все пользовательские классы соответствуют NSSecureCoding.

@implementation DLTree

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super init];
    if (self) {
        _name = [coder decodeObjectOfClass:[NSString class] forKey:@"DLTree_name"];
        _value = [coder decodeObjectOfClass:[NSObject class] forKey:@"DLTree_value"];
        _weight = [coder decodeObjectOfClass:[NSDecimalNumber class] forKey:@"DLTree_weight"];
        NSMutableDictionary *dict = [coder decodeObjectOfClass:[NSMutableDictionary class] forKey:@"DLTree_children"];
        _children = [dict objectForKey:@"children"];
        _sortHint = [coder decodeObjectOfClass:[NSData class] forKey:@"DLTree_sortHint"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:_name forKey:@"DLTree_name"];
    [coder encodeObject:_value forKey:@"DLTree_value"];
    [coder encodeObject:_weight forKey:@"DLTree_weight"];
    [coder encodeObject:[@{@"children": _children} mutableCopy] forKey:@"DLTree_children"];
    [coder encodeObject:_sortHint forKey:@"DLTree_sortHint"];
}

- (Class)classForCoder {
    return [self class];
}

+ (BOOL)supportsSecureCoding {
    return YES;
}

// ..
@end

Однако при декодировании я получаю следующую ошибку.

Printing description of err:
Error Domain=NSCocoaErrorDomain Code=4864 "*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)" 
UserInfo={NSDebugDescription=*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)}

Как сериализовать идесериализовать объекты используя NSKeyedArchiver?

...