UserDefaults/KeyedArchiver失败 [英] UserDefaults/KeyedArchiver Frustrations

查看:74
本文介绍了UserDefaults/KeyedArchiver失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个作业应用程序,该应用程序为每个作业使用自定义的作业对象.我试图将NSMutableArray(通过initWithArray广播到NSArray :)存储在standardUserDefaults中,但是在保存和重新加载数组时遇到了麻烦.

I'm working on a homework app that uses custom Assignment objects for each assignment. I am trying to store an NSMutableArray (casted to an NSArray via initWithArray:) in standardUserDefaults but I'm having trouble with saving and reloading the array.

我有一个表视图,您可以从中选择添加新任务(加载NewAssignmentViewController).保存分配后,会将其推回到AssigmentsViewController中的数组.然后,在每次加载显示分配的UITableView时都调用它.

I have a table view from which you can choose to add a new assignment (which loads NewAssignmentViewController). When you save the assignment, it is pushed back to an array in AssigmentsViewController. And then you call it every time you load the UITableView which shows the assignments.

以下是相关代码:

-(void)saveToUserDefaults:(NSArray*)myArray{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

if (standardUserDefaults) {
    [standardUserDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:@"Assignments"];
    [standardUserDefaults synchronize];
    }
}

-(void)retrieveFromUserDefaults{
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"Assignments"]; 
if (dataRepresentingSavedArray != nil) {
    NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
    if ([oldSavedArray count] != 0) {
        [assignments setArray:[[NSMutableArray alloc] initWithArray:oldSavedArray]];
    }
    else {
        assignments = [[NSMutableArray alloc] initWithCapacity:100];
    }
}
}
 -(void)backButtonPressed {
[self saveToUserDefaults:[[NSArray alloc] initWithArray:assignments]];
[self.navigationController popViewControllerAnimated:YES];
}

请帮助.它不会加载数组,但不会产生任何错误.一般而言,有关UserDefault或KeyedArchiver的任何提示将不胜感激.

Please help. It does not load the array but does not give any error. Any tips about UserDefault or KeyedArchiver in general would be greatly appreciated.

推荐答案

这里的事物:

如果我对您的理解正确,那么您正在尝试存储一个数组,其内容是赋值对象.

If I understand you correctly, you're trying store an array whose contents are the assignment objects.

如果要将这些对象序列化以存储到NSUserDefaults中,则Assignment对象本身需要通过覆盖以下方法来遵循NSCoding协议:

If you want to serialize these objects for storage into NSUserDefaults, the Assignment objects themselves need to conform the NSCoding protocol by overriding these methods:

- (void)encodeWithCoder:(NSCoder *)encoder;
- (id)initWithCoder:(NSCoder *)decoder;

由于您没有发布Assignment对象的代码,因此如果正确执行或根本不执行此操作,则不知道.如果有的话,您应该能够对对象进行编码.参见

Since you didn't post the code for your Assignment objects, dunno if you did this properly or at all. If you have you should be able to encode the object. See the Archives and Serializations Programming Guide for more.

至于NSUserDefaults,据我所读,您基本上是在尝试将应用程序的对象模型存储在那里.不是最好的主意.NSUserDefaults最适合用于轻量级的持久数据:基本首选项,字符串,通用数据的片段.

As for NSUserDefaults, by my read, you're basically trying to store your application's object model there. Not the best idea. NSUserDefaults is best suited for use with light-weight persistent data: basic preferences, strings, scraps of universal data.

我要做的是将存档数据写到文件中,并在视图加载时加载.

What I would do is write out your archived data to a file and load it when your view loads.

以下是该主题的开始iPhone开发的一些代码:

Here's some code from Beginning iPhone Development on that subject:

从符合NSCoding的一个或多个对象创建档案相对容易.首先,我们创建一个NSMutableData实例来保存编码数据,然后创建一个NSKeyedArchiver实例以将对象归档到该NSMutableData实例中:

Creating an archive from an object or objects that conforms to NSCoding is relatively easy. First, we create an instance of NSMutableData to hold the encoded data and then create an NSKeyedArchiver instance to archive objects into that NSMutableData instance:

NSMutableData *data = [[NSMutableData alloc] init]; 
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

在创建完这两个对象之后,我们然后使用键值编码来存档希望包含在存档中的所有对象,如下所示:

After creating both of those, we then use key-value coding to archive any objects we wish to include in the archive, like this:

[archiver encodeObject:myObject forKey:@"keyValueString"];

对所有要包含的对象进行编码后,我们只需告诉归档程序完成,将NSMutableData实例写入文件系统,然后对对象进行内存清理即可.

Once we’ve encoded all the objects we want to include, we just tell the archiver we’re done, write the NSMutableData instance to the file system, and do memory cleanup on our objects.

[archiver finishEncoding]; BOOL success = [data writeToFile:@"/path/to/archive" atomically:YES]; 
[archiver release]; 
[data release];

要重构档案中的对象,我们需要执行类似的过程.我们从存档文件创建一个NSData实例,并创建一个NSKeyedUnarchiver来解码数据:NSData * data = [[NSData alloc] initWithContentsOfFile:path];NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];之后,我们使用与存档对象相同的密钥从解存档器读取对象:

To reconstitute objects from the archive, we go through a similar process. We create an NSData instance from the archive file and create an NSKeyedUnarchiver to decode the data: NSData *data = [[NSData alloc] initWithContentsOfFile:path]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; After that, we read our objects from the unarchiver using the same key that we used to archive the object:

self.object = [unarchiver decodeObjectForKey:@"keyValueString"];

您还需要获取应用程序的文档目录保存和加载文件.

You'd also need to get your application's documents directory to save and load the files.

这本非常有用的书,里面充满了很多代码片段.关于持久性的章节可能对您有所帮助.使用Core Data来完成此任务可能会更快乐,想一想.

It's a wildly useful book, full of drop in code snippets. The chapter on persistence might be helpful for you. You might be much happier using Core Data for this task, come to think of it.

这篇关于UserDefaults/KeyedArchiver失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆