Pat*_*cia 7 iphone xcode objective-c nsuserdefaults ios
有没有办法让我"浏览" NSUserDefault我的iPhone应用程序中所有s 的列表,只删除某些?
例如,我想获得所有以某个单词开头的关键名称.
像这样的东西:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Dog*"];
Run Code Online (Sandbox Code Playgroud)
Jac*_*kin 18
你可以看看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
Run Code Online (Sandbox Code Playgroud)
用法:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH %@", @"something"];
[[NSUserDefaults standardUserDefaults] removeObjectsWithKeysMatchingPredicate:pred];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3237 次 |
| 最近记录: |