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

标签 iphone ios objective-c xcode nsuserdefaults

有没有办法让我“遍历”我的 iPhone 应用程序中所有 NSUserDefault 的列表,并且只删除某些?

例如,我想获取所有以某个单词开头的键名。

像这样:

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

最佳答案

可以翻看dictionaryRepresentation .

这是一个使用 NSPredicate 的实现作为通用匹配器以获得更大的灵 active 。

@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];

关于iphone - 删除以某个单词开头的所有 NSUserDefaults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3436792/

相关文章:

ios - Apple 拒绝第三方注册

iOS:找出小数位数

ios - UITableViewCell 问题的 Storyboard 布局

iOS 手机应用程序 : third party app integration like Viber/WhatsApp

iphone - Appcelerator 钛 : Android SDK doesn't load

iphone - 如何在后台运行 iOS4 应用程序?

ios - 将图像添加到我的 UIButton subview

ios - 解耦核心数据

ios - 如何使用 UIBezierPath 创建 UIImage

objective-c - 如何从nsarray删除空格,方括号和“