在 NSUserDefaults 中存储值有任何限制吗?
我已使用 NSUserDefaults
在我的应用程序中存储一些值。在 NSUserDefaults
中存储值有任何限制吗?
I have used NSUserDefaults
to store some values in my app. Is there any limit for storing values in NSUserDefaults
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
只要 iPhone/iPad 上有足够的空间,您就可以存储 NSUserDefault 值。所有这些值都存储到一个 .plist 文件中,并且这个文件非常小,大多数时候都在 1 kb 以下(除非您存储大量数据)。
As long as there's enough space on the iPhone/iPad, you can store NSUserDefault values. All those values is stored into a .plist file, and this file is very small, most of the time under 1 kb (unless you store a lot of data).
您可以存储的类型有限制:它们必须都是属性列表对象,即
NSString
、NSNumber
、NSData
、NSArray
和NSDictionary
。此外,如果值也是属性列表对象,则只能存储NSArray
和NSDictionary
;另外,NSDictionary
的所有键都必须是字符串。请注意,像
UIColor
这样的对象不在上面的列表中。因此,如果您想在默认数据库中存储颜色,则需要首先将其转换为字符串或数据对象,然后在读取默认值时将其转换回来。就大小限制而言,没有记录,但请注意,所有数据都将存储为属性列表文件。整个文件是作为一个整体读入和写出的,因此如果您使用 NSUserDefaults 存储大量仅部分更改的数据,您将浪费大量时间进行不必要的 I/奥。
There are limits on what types you may store: they must all be Property List objects, namely
NSString
,NSNumber
,NSData
,NSArray
, andNSDictionary
. Furthermore, you may only storeNSArray
andNSDictionary
if the values are also property list objects; also, all the keys of theNSDictionary
must be strings.Note that an object like
UIColor
is not on the above list. So if you want to store a color in the defaults database, you'll need to convert it into a string or data object first, then convert it back when you read the defaults.As far as size limits, there are none that are documented, but note that all data will be stored as a property list file. The entire file is read in and written out as a whole, so if you use
NSUserDefaults
to store a large amount of data that only changes in parts, you will be wasting a lot of time doing unnecessary I/O.来自iOS SDK代码,以及相关Apple官方文档。
摘要
From iOS SDK codes, and related Apple official document..
Summary
每个人都回答了“有限制吗?”这个直接问题。然而,我发现这个帖子确实想要理解“在 UserDefaults 中存储多少才算太多?”
如果您正在寻找答案,这里有一个有用的主题。我发现有用的答案是转到您的项目文件并查看 plist 文件大小:
重要的是要记住:
上述内容减轻了我的担忧,即我越来越多的默认值(现在大约 20-25 个)会导致问题。我已经使用 CoreData,所以我正在考虑使用哪个,因为允许的用户首选项/自定义的数量越来越长。因此,我将保留用户默认设置。
但是,正如其他答案所指出的那样,该文件将作为一个整体来读取和写入。因此,读取 20 个键/字符串字典和 5 个键/布尔字典只是为了检索一个字符串……这并不理想。尽管如此,如果它不会影响性能并且可以节省大量代码,为什么不呢?
Everyone has answered the direct question of "is there a limit?" However, I found this thread really looking to understand "how much is too much to store in UserDefaults?"
If you're looking for that answer, here's a useful thread. The responses I found helpful were to go to your project file and look at the plist file size:
Important to remember:
The above alleviated my concerns that my growing number of defaults (about 20-25 now) would cause problems. I use CoreData already, so I was considering which to use since my number of allowed user preferences/customizations is growing long. So, I'm going to stay with user defaults.
However, as other answers have pointed out, the file will be read and written as a whole. So reading 20 key/string dictionaries and 5 key/boolean dictionaries just to retrieve one string... not exactly ideal. Nonetheless, if it doesn't hurt performance and it saves you a ton of code, why not?
正如许多人已经提到的:我不知道在 .plist 中存储数据(例如 UserDefaults)有任何 SIZE 限制(物理内存除外)。所以这不是多少的问题。
真正的问题应该是您多久写入一次新的/更改的值...这与写入将导致的电池消耗有关。
如果单个值发生更改,IOS 无法避免物理写入“磁盘”,只是为了保持数据完整性。对于 UserDefaults,这会导致整个文件重写到磁盘。
这会为“磁盘”供电并使其保持较长时间的供电状态,并防止 IOS 进入低功耗状态。
读取没有问题,因为所有值都缓存在内存中。
编辑:(2019 年 7 月):我刚刚发现 Jeffry Fulton 写的这篇非常好的博客文章。
https://jeffreyfulton.ca/blog/2018/02/userdefaults-限制和替代方案
他详细描述了用户默认值的不同方面,并撰写了一些性能测试。
快乐编码!!!
As many already mentioned: I'm not aware of any SIZE limitation (except physical memory) to store data in a .plist (e.g. UserDefaults). So it's not a question of HOW MUCH.
The real question should be HOW OFTEN you write new / changed values... And this is related to the battery drain this writes will cause.
IOS has no chance to avoid a physical write to "disk" if a single value changed, just to keep data integrity. Regarding UserDefaults this cause the whole file rewritten to disk.
This powers up the "disk" and keep it powered up for a longer time and prevent IOS to go to low power state.
READS are no problem, as all values are cached in memory.
EDIT: (July 2019): I just found this very good blog post by Jeffry Fulton.
https://jeffreyfulton.ca/blog/2018/02/userdefaults-limitations-and-alternatives
He describes in detail the different aspects of the user defaults and also writes about some performance tests.
Happy Coding!!!
从 iPadOS 13.1 beta 3 开始,我在尝试存储较大的对象(图像)时看到以下消息。
但是检索密钥似乎仍然有效。
As of iPadOS 13.1 beta 3, I'm now seeing the following message when trying to store a larger object (an image).
However retrieving the key appears to still work.
NSUserDefaults 中存储值没有限制。
There is No Limit for storing values in NSUserDefaults..
NSUserDefaults
中唯一的存储限制是设备存储容量。只要
iOS
设备 中有足够多的可用存储空间,您实际上就可以在中存储数据NSUserDefaults
。 键-值对存储在 xml 结构化文件 (.plist) 中,该文件存储在 App Bundle 中。The only Storage Limitation in
NSUserDefaults
is the Device Storage Capacity.As much as there are available Storage Space in an
iOS
Device, you can practically store data inNSUserDefaults
. The key-value pair is stored on a xml structured file (.plist) which is stored in an App Bundle.据我所知,NSUserdefaults 中的存储没有限制。
As far as my knowledge there is no limit for storing in NSUserdefaults.
无论驱动器上允许的最大文件大小如何。你可以用这段代码来检查一下!
It whatever the maximum allowed file size is on the drive. You can use this piece of code to check it out!