从 NSUserDefaults 字典 iOS 中删除所有键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6797096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete all keys from a NSUserDefaults dictionary iOS
提问by Tono Nam
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 字典来存储基本信息,例如高分等,以便在用户关闭应用程序时不会丢失数据。无论如何我使用:
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 enableyou to store booleans, integers, objectsetc. what method would I have to execute to delete all keys so that NSUserDefaults becomes like the fist time I launch the app?
反正问题是,我储存很多其他的事情,因为NSUserDefaults enable你到店里booleans,integers,objects等我有什么方法来执行删除所有键,以便成为NSUserDefaults的像拳头时我启动应用程序?
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....
我仍然能够检索到高分....
回答by Alex Nichol
If you have a look at the NSUserDefaults documentationyou 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:
如果您查看NSUserDefaults 文档,您将看到一个方法- (NSDictionary *) dictionaryRepresentation。在标准用户默认设置上使用此方法,您可以获得用户默认设置中所有键的列表。然后,您可以使用它来清除用户默认值:
- (void)resetDefaults {
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
[defs removeObjectForKey:key];
}
[defs synchronize];
}
回答by skywinder
Shortest way to do this with the same resultslike in Alex Nichol's top answer:
使用与Alex Nichol 的最佳答案相同的结果来执行此操作的最短方法:
NSString *appDomain = NSBundle.mainBundle.bundleIdentifier;
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
[[NSUserDefaults standardUserDefaults] synchronize];
回答by Cameron E
One-liner:
单线:
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:NSBundle.mainBundle.bundleIdentifier];
回答by Sourabh Sharma
Simple Solution
简单的解决方案
Objective C:
目标 C:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
In Swift 3.0 :
在Swift 3.0 中:
if let appDomain = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: appDomain)
}
回答by superarts.org
Swift version:
迅捷版:
if let bid = NSBundle.mainBundle().bundleIdentifier {
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(bid)
}
回答by superarts.org
+ (void) resetStandardUserDefaultsdoesn't persist the changes, it simply resets the in-memory user defaults object so that the next synchronizecall will read from the on-disk copy, instead of overwriting existing in-memory values with the on-disk versions.
+ (void) resetStandardUserDefaults不保留更改,它只是重置内存中用户默认对象,以便下一次synchronize调用将从磁盘副本中读取,而不是用磁盘版本覆盖现有的内存中值。
Iterating over the keys is better, but there's actually a function that does this for you: removePersistentDomainForName:.
迭代键更好,但实际上有一个函数可以为您执行此操作:removePersistentDomainForName:.
// you can usually get the domain via [[NSBundle mainBundle] bundleIdentifier]
[[NSUserDefaults standardUserDefaults]
removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
// or use a string for any other settings domains you use
//[[NSUserDefaults standardUserDefaults]
// removePersistentDomainForName:@"com.mycompany.myappname"];
[[NSUserDefaults standardUserDefaults] synchronize];
At the end of the synchronizeoperation, both the disk and memory copies of user defaults will contain none of the values set by your application.
在synchronize操作结束时,用户默认值的磁盘和内存副本都将不包含应用程序设置的任何值。
回答by netshark1000
Oneliner in Swift:
Swift 中的 Oneliner:
Swift 3
斯威夫特 3
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(
NSBundle.mainBundle().bundleIdentifier!)
Swift 4
斯威夫特 4
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
回答by bogen
For those of you that want to do this in the test target, use this (as the removePersistentDomaindoes not work for that case)
对于那些想要在测试目标中执行此操作的人,请使用它(因为removePersistentDomain不适用于这种情况)
Swift 3:
斯威夫特 3:
for key in Array(UserDefaults.standard.dictionaryRepresentation().keys) {
UserDefaults.standard.removeObject(forKey: key)
}
回答by Taichi Kato
For Swift 3:
对于 Swift 3:
let appDomain = Bundle.main.bundleIdentifier!
UserDefaults.standard.removePersistentDomain(forName: appDomain)
回答by Jay Mehta
For Swift 3:
对于 Swift 3:
if let bundle = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: bundle)
}

