用户默认值/KeyedArchiver 挫败感
我正在开发一个作业应用程序,该应用程序为每个作业使用自定义作业对象。我试图在 standardUserDefaults 中存储 NSMutableArray (通过 initWithArray 转换为 NSArray:),但在保存和重新加载数组时遇到问题。
我有一个表视图,您可以从中选择添加新分配(加载 NewAssignmentViewController)。当您保存作业时,它会被推回到 AssigmentsViewController 中的数组中。然后每次加载显示分配的 UITableView 时都会调用它。
这是相关代码:
-(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 一般的提示将不胜感激。
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.
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.
Here is the relating code:
-(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];
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有几件事:
如果我理解正确的话,您正在尝试存储一个其内容是赋值对象的数组。
如果您想将这些对象序列化以存储到 NSUserDefaults 中,则赋值对象本身需要通过覆盖这些方法来符合 NSCoding 协议:
由于您没有发布赋值对象的代码,所以不知道您是否正确执行了此操作或根本没有执行此操作。如果你有,你应该能够对对象进行编码。请参阅 档案和序列化编程指南了解更多信息。
至于 NSUserDefaults,根据我的阅读,您基本上是在尝试将应用程序的对象模型存储在那里。这不是最好的主意。 NSUserDefaults 最适合与轻量级持久数据一起使用:基本首选项、字符串、通用数据片段。
我要做的是将存档数据写到一个文件中,并在加载视图时加载它。
以下是开始 iPhone 开发中关于该主题的一些代码:
您还需要获取您的 应用程序的文档目录,用于保存和加载文件。
这是一本非常有用的书,充满了代码片段。关于坚持的章节可能对你有帮助。想想看,您可能会更高兴使用 Core Data 来完成此任务。
Couple of things here:
If I understand you correctly, you're trying store an array whose contents are the assignment objects.
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:
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.
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.
Here's some code from Beginning iPhone Development on that subject:
You'd also need to get your application's documents directory to save and load the files.
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.
我不确定这是否能解决您的问题,但您不必将数组作为 NSData 从默认值中取出。检查 NSUserDefaults 参考 你会看到数组是有效的默认对象。
I'm not sure if this will fix your problem, but you don't have to pull the array out of Defaults as NSData. Check the NSUserDefaults reference and you'll see that Arrays are valid default objects.