删除所有以某个词开头的NSUserDefaults

有没有办法让我“走”通过我的iPhone应用程序中的所有NSUserDefault列表,只删除某些?

例如,我想要获得以某个单词开始的所有关键名称。

像这样的东西:

 [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Dog*"]; 

你可以查看dictionaryRepresentation

下面是一个使用NSPredicate作为通用匹配器来实现更大灵活性的实现。

 @interface NSUserDefaults (JRAdditions) - (void)removeObjectsWithKeysMatchingPredicate:(NSPredicate *)predicate; @end @implementation NSUserDefaults (JRAdditions) - (void)removeObjectsWithKeysMatchingPredicate:(NSPredicate *)predicate { NSArray *keys = [[self dictionaryRepresentation] allKeys]; for(NSString *key in keys) { if([predicate evaluateWithObject:key]) { [self removeObjectForKey:key]; } } } @end 

用法:

 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH %@", @"something"]; [[NSUserDefaults standardUserDefaults] removeObjectsWithKeysMatchingPredicate:pred]; 

我得到了解决scheme,只是用它在你想要删除所有会议

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

它会工作