简体   繁体   English

未存档对象没有存档对象的类描述符

[英]Unarchived object doesn't have the class descriptor of the archived object

I'm was writing unit tests and when I encountered a strange behaviour of NSKeyedArchiver/NSKeyedUnarchiver. 我正在编写单元测试,并且遇到NSKeyedArchiver / NSKeyedUnarchiver的异常行为。 The following is a failing test. 以下是失败的测试。 Am I doing wrong? 我做错了吗?

Here is a simple project to play with: https://github.com/genesislive2007/Archiving-Headache 这是一个简单的项目: https : //github.com/genesislive2007/Archiving-Headache

-(void)test {

    NSData* serializedObject = [NSKeyedArchiver archivedDataWithRootObject:[Drug new]];

    id deserializedObject = [NSKeyedUnarchiver unarchiveObjectWithData:serializedObject];

    XCTAssertTrue([deserializedObject isKindOfClass:[Drug class]]);

}

UPDATE 更新

Some logs: 一些日志:

NSLog(@"Class as string: %@", NSStringFromClass([deserializedObject class]));
NSLog(@"Address of the static class: %p", [Drug class]);
NSLog(@"Address of the object class: %p", [deserializedObject class]);

Archiving Headache[1638:60b] Class as string: Drug 存档头痛[1638:60b]分类为字符串:毒品

Archiving Headache[1638:60b] Address of the static class: 0x8bfd1d0 存档头痛[1638:60b]静态类的地址:0x8bfd1d0

Archiving Headache[1638:60b] Address of the object class: 0x357c 存档头痛[1638:60b]对象类的地址:0x357c

As you see above the addreses to the class descriptiors are different. 如上所示,类描述符的地址不同。

This has to do with the mechanisms of unarchiving, in this case it has to do with shallow copy and deep copy. 这与取消存档的机制有关,在这种情况下,与浅拷贝和深拷贝有关。 When you unarchive an object and assign it to the respective return type you are creating a DEEP COPY or the archived object, a new object who's information such as its hierarchical structure and instance variables are stored in the specified folder with the given key since your using a keyed archiver, when returned will essentially make a new object/instance of the class that was archived thereby allowing you to create a deep copy and to properly restore the object in the state it was archived in as, which is the reason you are getting a new address. 当您取消归档对象并将其分配给相应的返回类型时,您将创建DEEP COPY或已归档对象,自您使用以来,具有给定密钥的新对象(其层级结构和实例变量等信息)将存储在指定的文件夹中。一个有键的存档器,当返回时,实际上将创建一个已存档类的新对象/实例,从而使您可以创建深层副本,并以该对象的存档状态正确还原该对象,这就是您得到此文件的原因一个新地址。 Program 19.10 计划19.10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM