从 NSUserDefaults 字典 iOS 中删除所有键
我使用 NSUserDefaults 字典来存储基本信息,例如高分等,以便当用户关闭应用程序时数据不会丢失。无论如何,我使用:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
存储数据。例如,如果我希望存储一个新的高分,那么我会这样做:
[prefs setInteger:1023 forKey:@"highScore"];
[prefs synchronize]; //this is needed in case the app is closed.
稍后,如果我希望检索高分,我会这样做:
[prefs integerForKey:@"highScore"];
无论如何,重点是我存储了很多其他内容,因为 NSUserDefaults enable< /code> 你要存储
booleans
、integers
、objects
等。我必须执行什么方法才能删除所有键,以便 NSUserDefaults 变得像我第一次启动该应用程序?
我正在寻找类似的东西:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs deleteAllKeysAndObjectsInTheDictionary];
或者也许有一种方法可以获取所有键,我必须循环遍历每个对象,但我不知道如何删除它们。
编辑:
我已经尝试过:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[NSUserDefaults resetStandardUserDefaults];
[prefs synchronize];
并且我仍然能够检索高分......
I use the NSUserDefaults dictionary to store basic information such as high scores etc so that when the user closes the app data is not lost. Anyways I use:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
to store data. If I wish to store a new high score for example then I would do:
[prefs setInteger:1023 forKey:@"highScore"];
[prefs synchronize]; //this is needed in case the app is closed.
and later if I wish to retrieve the high score I would do:
[prefs integerForKey:@"highScore"];
anyways the point is that I store a lot of other things because the NSUserDefaults enable
you to store booleans
, integers
, objects
etc. what method would I have to execute to delete all keys so that NSUserDefaults becomes like the fist time I launch the app?
I am looking for something like:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs deleteAllKeysAndObjectsInTheDictionary];
or maybe there is a way of getting all keys and I have to loop through each object but I don't know how to remove them.
EDIT:
I have tried :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[NSUserDefaults resetStandardUserDefaults];
[prefs synchronize];
and I still am able to retrieve a high score....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
如果您查看 NSUserDefaults 文档 你会看到一个方法
- (NSDictionary *)dictionaryRepresentation
。在标准用户默认值上使用此方法,您可以获得用户默认值中所有键的列表。然后您可以使用它来清除用户默认值:If you have a look at the NSUserDefaults documentation you will see a method
- (NSDictionary *) dictionaryRepresentation
. Using this method on the standard user defaults, you can get a list of all keys in the user defaults. You can then use this to clear the user defaults:获得相同结果的最短方法,如 Alex Nichol 的最佳答案:
Shortest way to do this with the same results like in Alex Nichol's top answer:
单线:
One-liner:
简单解决方案
目标 C:
Swift 3.0 到 Swift 5.0:
Simple Solution
Objective C:
Swift 3.0 to Swift 5.0 :
斯威夫特版本:
Swift version:
+ (void)resetStandardUserDefaults
不会保留更改,它只是重置内存中的用户默认对象,以便下一个synchronize
调用将从磁盘副本中读取,而不是用磁盘上的版本覆盖现有的内存值。迭代键更好,但实际上有一个函数可以为您执行此操作:
removePersistentDomainForName:
。在
synchronize
操作结束时,用户默认值的磁盘和内存副本将不包含应用程序设置的任何值。+ (void) resetStandardUserDefaults
doesn't persist the changes, it simply resets the in-memory user defaults object so that the nextsynchronize
call will read from the on-disk copy, instead of overwriting existing in-memory values with the on-disk versions.Iterating over the keys is better, but there's actually a function that does this for you:
removePersistentDomainForName:
.At the end of the
synchronize
operation, both the disk and memory copies of user defaults will contain none of the values set by your application.Swift 中的 Oneliner:
Swift 3
Swift 4
Oneliner in Swift:
Swift 3
Swift 4
对于那些想要在测试目标中执行此操作的人,请使用它(因为
removePersistentDomain
不适用于这种情况)Swift 3:
For those of you that want to do this in the test target, use this (as the
removePersistentDomain
does not work for that case)Swift 3:
对于 Swift 3:
For Swift 3:
对于斯威夫特 3:
For Swift 3:
我发现将代码放在
UserDefaults
的扩展中是最方便的。Swift 5
用法
I found it the most handy to place the code in an extension on
UserDefaults
.Swift 5
Usage
斯威夫特
放在你的逻辑中
Swift
place in your logic
迅速
Swift
此方法不这样做吗:
来自
NSUserDefaults
的文档:基于此,您可以执行以下操作:
现在应该重置默认值。
Does this method not do that:
From the documentation for
NSUserDefaults
:Based on this, you can do:
and now the defaults should be reset.
Swift 3 或 4
我们甚至可以将描述的片段简化为这个现代表达:
Swift 3 or 4
We can even simplify described snippet into this modern expression:
在 Swift 5.0 下面,单行代码就足够了。
In Swift 5.0 below single line of code is enough.
快速删除所有 UserDefault 值(最新语法)
To remove all UserDefault value in swift (Latest syntax)
我用这个:
I use this: