简体   繁体   English

解压缩使用encodeRootObject:归档的内容的正确方法是什么?

[英]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. 如果确实使用NSKeyedArchiverNSKeyedUnarchiver ,则建议您遵循该惯例,并为对象编写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.

相关问题 从NSuserDefaults取消存档数据的正确方法是什么? - What is the correct way to unarchive data from NSuserDefaults? 为什么我似乎无法在由OS X应用程序存档的iPhone应用程序中取消归档? - Why can't I seem to unarchive objects in an iPhone app that were archived by an OS X app? 使用ARC为ios声明readonly属性的正确方法是什么 - What is the correct way to declare a readonly property for ios using ARC 使用editActionsForRowAtIndexPath时,删除UITableViewCell的显示滑动的正确方法是什么? - What is the correct way to remove the revealed swipe of a UITableViewCell when using editActionsForRowAtIndexPath:? 使用NSURLSession进行POST,发布变量的正确方法是什么? - Using NSURLSession to POST, what is the correct way to post the variables? 正确使用@autoreleasepools的方法? - Correct way of using @autoreleasepools? 进行布尔运算的正确方法是什么? - What is the correct way to proceed with this bool? 使用NSLocalizedString的正确方法是什么? - What is the correct way to use NSLocalizedString? 在iOS上使用带有ARC的块时,确保对象存在并且不会泄漏的正确方法是什么? - What is the correct way to be sure that the object will be there and it won't leak while using blocks with ARC on iOS? 调用[super layoutSubviews]的正确方法是什么? - What is the correct way to call [super layoutSubviews]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM