iOS NSDictionary 保存到 NSUserDefaults 不保存值

编程入门 行业动态 更新时间:2024-10-28 19:35:02
本文介绍了iOS NSDictionary 保存到 NSUserDefaults 不保存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我的 NSDictionary 有 NSStrings 用于 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 不保存值

本文发布于:2023-07-16 14:42:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1122721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不保存   iOS   NSDictionary   NSUserDefaults

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!