ios 清除 NSUserDefaults
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/545091/
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
Clearing NSUserDefaults
提问by TonyNeallon
I'm using +[NSUserDefaults standardUserDefaults]to store application settings. This consists of roughly a dozen string values. Is it possible to delete these values permanently instead of just setting them to a default value?
我+[NSUserDefaults standardUserDefaults]用来存储应用程序设置。这包括大约十几个字符串值。是否可以永久删除这些值而不是将它们设置为默认值?
回答by Christopher Rogers
You can remove the application's persistent domain like this:
您可以像这样删除应用程序的持久域:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
In Swift 3 and later:
在 Swift 3 及更高版本中:
if let bundleID = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: bundleID)
}
This is similar to the answer by @samvermette but is a little bit cleaner IMO.
这与@samvermette 的答案相似,但 IMO 更简洁一些。
回答by samvermette
This code resets the defaults to the registration domain:
此代码将默认值重置为注册域:
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
In other words, it removeObjectForKeyfor every single key you ever registered in that app.
换句话说,它removeObjectForKey适用于您在该应用程序中注册的每个密钥。
Credits to Ken Thomases on this Apple Developer Forums thread.
回答by sbooth
Did you try using -removeObjectForKey?
您是否尝试使用 - removeObjectForKey?
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"defunctPreference"];
回答by juliensaad
Here is the answer in Swift:
这是斯威夫特的答案:
let appDomain = NSBundle.mainBundle().bundleIdentifier!
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)
回答by Roger Sanoli
If you need it while developing, you can also reset your simulator, deleting all the NSUserDefaults.
如果您在开发时需要它,您还可以重置您的模拟器,删除所有NSUserDefaults.
iOS Simulator -> Reset Content and Settings...
iOS 模拟器 -> 重置内容和设置...
Bear in mind that it will also delete all the apps and files on simulator.
请记住,它还会删除模拟器上的所有应用程序和文件。
回答by folse
NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
for (NSString *key in [defaultsDictionary allKeys]) {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}
回答by Adam Waite
In Swift:
在斯威夫特:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.dictionaryRepresentation().keys.forEach { defaults.removeObjectForKey(extension NSUserDefaults {
func clear() {
guard let domainName = NSBundle.mainBundle().bundleIdentifier else {
return
}
self.removePersistentDomainForName(domainName)
}
}
) }
回答by Valentin Shergin
I love extensions when they make the code cleaner:
我喜欢使代码更简洁的扩展:
extension UserDefaults {
func clear() {
guard let domainName = Bundle.main.bundleIdentifier else {
return
}
removePersistentDomain(forName: domainName)
synchronize()
}
}
Swift 5
斯威夫特 5
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
回答by Hemang
Note: This answer has been updated for Swift too.
注意:此答案也已针对 Swift 进行了更新。
What about to have it on one line?
把它放在一条线上怎么样?
Extending @Christopher Rogers answer – the accepted one.
扩展@Christopher Rogers 的回答——已被接受的回答。
[[NSUserDefaults standardUserDefaults] synchronize];
and yes, sometime you may need to synchronizeit,
是的,有时你可能需要synchronize它,
- (void) clearDefaults {
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
I've created a method to do this,
我已经创建了一个方法来做到这一点,
extension UserDefaults {
class func clean() {
guard let aValidIdentifier = Bundle.main.bundleIdentifier else { return }
standard.removePersistentDomain(forName: aValidIdentifier)
standard.synchronize()
}
}
Swift?
斯威夫特?
With swift its even more easy.
有了 swift 就更容易了。
UserDefaults.clean()
And usage:
和用法:
osascript -e 'tell application "iOS Simulator" to quit'
xcrun simctl list devices | grep -v '^[-=]' | cut -d "(" -f2 | cut -d ")" -f1 | xargs -I {} xcrun simctl erase "{}"
回答by Daniel Gomez Rico
I found this:
我找到了这个:
##代码##Source: https://gist.github.com/ZevEisenberg/5a172662cb576872d1ab
来源:https: //gist.github.com/ZevEisenberg/5a172662cb576872d1ab

