Clearing NSUserDefaults
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?
You can remove the application's persistent domain like this:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
In Swift 3 and later:
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.
This code resets the defaults to the registration domain:
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
In other words, it removeObjectForKey
for every single key you ever registered in that app.
Credits to Ken Thomases on this Apple Developer Forums thread.
你尝试使用 - removeObjectForKey
?
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"defunctPreference"];
链接地址: http://www.djcxy.com/p/85160.html
上一篇: 将NSDate转换为NSString
下一篇: 清除NSUserDefaults