[英]What is the correct way to unarchive something that was archived using encodeRootObject:?
I am attempting to use the Keyed Archiver classes for the first time and I'm failing the last assert in this simple test (OCUnit): 我尝试首次使用Keyed Archiver类,但在此简单测试(OCUnit)中,我未通过最后一个断言:
- (void) testNSCoding
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
[dict setObject:@"hello" forKey:@"testKey"];
NSMutableData* data = [NSMutableData data];
NSKeyedArchiver *ba = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[ba encodeRootObject:dict];
[ba finishEncoding];
STAssertTrue(data.length != 0, @"Archiver gave us nothing.");
NSKeyedUnarchiver *bua = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
id decodedEntity = [bua decodeObjectForKey:@"root"];
[bua finishDecoding];
STAssertNotNil(decodedEntity, @"Unarchiver gave us nothing.");
}
I have confirmed that the archiver is archiving, I'm assuming the issue exists in the unarchiving. 我已经确认归档程序正在归档,我假设问题在归档中存在。 According to the Archives and Serializations Guide I believe that perhaps there is some issue with how I am using the Unarchiver?
根据《 档案和序列化指南》,我认为我如何使用Unarchiver可能存在一些问题?
Thanks! 谢谢!
First, you should not use the encodeRootObject
method. 首先,您不应使用
encodeRootObject
方法。 That's a legacy method defined in NSCoder
to support obsolete non-keyed archivers, and can only be decoded using decodeObject:
. 这是
NSCoder
定义的传统方法,用于支持过时的非键存档程序,并且只能使用decodeObject:
进行解码。 You only use the pair of encodeObjectForKey:
and decodeObjectForKey:
. 您只能使用一对
encodeObjectForKey:
和decodeObjectForKey:
So, 所以,
id decodedEntity = [bua decodeObjectForKey:@"root"];
should be 应该
id decodedEntity = [bua decodeObjectForKey:@"testKey"];
If you want to decode the totality of a dictionary, instead of 如果要解码字典的全部,而不是
[ba encodeRootObject:dict];
do 做
[ba encodeObject:dict forKey:@"root"];
By the way, for simple purposes it often suffices to use NSUserDefaults
, which automatically takes care of creating the file to write on, writing things on the file, and reading it when the program is launched the next time. 顺便说一句,出于简单的目的,通常使用
NSUserDefaults
就足够了,它会自动负责创建要写入的文件,在文件上写东西以及在下次启动程序时读取它。
If you just need to encode a dictionary, using NSPropertyListSerialization
usually suffices. 如果只需要对字典进行编码,
NSPropertyListSerialization
通常可以使用NSPropertyListSerialization
。
If you do use NSKeyedArchiver
and NSKeyedUnarchiver
, I recommend you to follow the practice and write encodeWithCoder:
and initWithCoder:
for an object. 如果确实使用
NSKeyedArchiver
和NSKeyedUnarchiver
,则建议您遵循该惯例,并为对象编写encodeWithCoder:
和initWithCoder:
encodeWithCoder:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id profileData = [defaults dataForKey:kProfileDataKey]; // or you can get it from the web
if (profileData && [profileData isKindOfClass:[NSData class]]) {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:(NSData *)profileData];
unarchiver.requiresSecureCoding = YES; // <NSSecureCoding>
id object = [unarchiver decodeObjectOfClass:[MyClass class] forKey:NSKeyedArchiveRootObjectKey];
NSLog(@"%@", object);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.