【发布时间】:2017-03-19 11:28:17
【问题描述】:
类未正确解档。 请参阅下面的代码示例。
@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" 但对象是 NSMutableDictionary 而不是 A 类。并且在 unarchiveObjectWithData 调用时不会发生 initWithCoder 调用。 那么,make it code 是如何工作的呢?为什么不反序列化继承 NSMutableDictionary 的类?
【问题讨论】:
标签: objective-c nsmutabledictionary nscoding nskeyedarchiver nskeyedunarchiver