iOS NSDictionary 保存到 NSUserDefaults 不保存值 [英] iOS NSDictionary saved to NSUserDefaults not saving values

查看:52
本文介绍了iOS NSDictionary 保存到 NSUserDefaults 不保存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将带有数组值的 NSDictionary 保存到 NSUserDefaults,但遇到了一些奇怪的问题.

I am trying to save a NSDictionary with array values to NSUserDefaults but am having some strange trouble.

我的 NSDictionaryNSStrings 用于 keys 并且每个 value 都是一个 NSArrayNSNumbers.当我把字典打印出来时,一切都很好.我把这本字典写到 NSUserDefaults ,如果我马上把它读回来,一切都很好.使用这个一切都很好:

My NSDictionary has NSStrings for keys and each value is a NSArray of NSNumbers. When I print the dictionary out, everything is fine. I write this dictionary to NSUserDefaults and if I read it back out right away, everything seams fine. Using this everything seams just fine:

[[NSUserDefaults standardUserDefaults] setObject:self.selectedOptionPositions 
                                          forKey:PREF_OPTIONS_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];

//THIS PRINT EVERYTHING OUT EXACTLY AS IT SHOULD!       
NSLog(@"read after write: %@", [[NSUserDefaults standardUserDefaults] 
                                 objectForKey:PREF_OPTIONS_KEY]);

当我创建处理此问题的类的新实例时,问题就出现了.当我创建类的新实例并在 init 方法中检查 NSDictionary 时,如下所示:

The problem comes when I create a new instance of the class that handles this. When I make a new instance of the class and in the init method check the NSDictionary like so:

NSLog(@"read initial: %@", [[NSUserDefaults standardUserDefaults] 
                              objectForKey:PREF_OPTIONS_KEY]);

当我打印日志时,NSDictionary 包含所有 ,但所有 values 现在都是空的!重新创建类后,所有新添加的 keys 都存在,但没有 values 持续存在.

When I print that logging, the NSDictionary contains all of the keys but all of the values are now empty! All newly added keys exist after recreating the class, but no values persist.

这里有什么问题?控制台中没有警告或错误.

What could be wrong here? There are no warnings or errors in the console.

推荐答案

试试这个:

您可以使用 NSKeyedArchiver 将您的字典写入 NSData,您可以将其存储在首选项中.

You can use NSKeyedArchiver to write out your dictionary to an NSData, which you can store among the preferences.

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.selectedOptionPositions];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:PREF_OPTIONS_KEY];

用于检索数据:

NSData *dictionaryData = [defaults objectForKey:PREF_OPTIONS_KEY];
NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:dictionaryData];

iOSNSKeyedArchiver 的开发者文档 它说:

NSKeyedArchiver,NSCoder 的具体子类,提供了一种方法将对象(和标量值)编码为与体系结构无关的可以存储在文件中的格式.当您归档一组对象,每个对象的类信息和实例变量被写入存档.NSKeyedArchiver 的配套类,NSKeyedUnarchiver,解码存档中的数据并创建一组对象等价于原始集合.

NSKeyedArchiver, a concrete subclass of NSCoder, provides a way to encode objects (and scalar values) into an architecture-independent format that can be stored in a file. When you archive a set of objects, the class information and instance variables for each object are written to the archive. NSKeyedArchiver’s companion class, NSKeyedUnarchiver, decodes the data in an archive and creates a set of objects equivalent to the original set.

这篇关于iOS NSDictionary 保存到 NSUserDefaults 不保存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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