7
有没有办法让我在我的iPhone应用程序中“遍历”所有NSUserDefault
的列表,并且只删除某些列表?删除所有以某个词开始的我的NSUserDefaults
例如,我想要获取以某个单词开头的所有关键名称。
事情是这样的:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Dog*"];
有没有办法让我在我的iPhone应用程序中“遍历”所有NSUserDefault
的列表,并且只删除某些列表?删除所有以某个词开始的我的NSUserDefaults
例如,我想要获取以某个单词开头的所有关键名称。
事情是这样的:
[[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];
我得到了这个解决方案要删除所有会话
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
只是用它,将工作