我使用+[NSUserDefaults standardUserDefaults]来存储应用程序设置。它由大约12个字符串值组成。是否可以永久删除这些值,而不是将它们设置为默认值?


当前回答

这段代码将默认值重置为注册域:

[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];

换句话说,它为你在那个应用中注册的每一个键removeObjectForKey。

感谢Ken Thomases在苹果开发者论坛上的发言。

其他回答

你可以像这样删除应用程序的持久域:

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

在Swift 3及以后版本中:

if let bundleID = Bundle.main.bundleIdentifier {
    UserDefaults.standard.removePersistentDomain(forName: bundleID)
}

这与@samvermette的回答相似,但在我看来更简洁一些。

这段代码将默认值重置为注册域:

[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];

换句话说,它为你在那个应用中注册的每一个键removeObjectForKey。

感谢Ken Thomases在苹果开发者论坛上的发言。

所有以上的答案都是非常相关的,但如果有人仍然无法重置用户默认删除的应用程序,那么你可以重置模拟器的内容设置,它将工作。

NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
for (NSString *key in [defaultsDictionary allKeys]) {
                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}

你试过使用-removeObjectForKey吗?

 [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"defunctPreference"];