加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 01:48:05 所属栏目:百科 来源:网络整理
导读:有没有办法让我“遍历”我的iPhone应用程序中的所有NSUserDefaults列表,只删除某些? 例如,我想获得所有以某个单词开头的关键名称. 像这样的东西: [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Dog*"]; 解决方法 你可以浏览一下 dictionar
有没有办法让我“遍历”我的iPhone应用程序中的所有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];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读