开发者

Delete all my NSUserDefaults that start with a certain word

开发者 https://www.devze.com 2023-01-10 14:04 出处:网络
Is there a way for me to \"walk\" through a list of all the NSUserDefaults in my iPhone app, and only delete certain ones?

Is there a way for me to "walk" through a list of all the NSUserDefaults in my iPhone app, and only delete certain ones?

For example, I'd like to get all the key names that start with a certain word.

Something like this:

[[NSUser开发者_如何学PythonDefaults standardUserDefaults] removeObjectForKey:@"Dog*"];


You can look through the dictionaryRepresentation.

Here's an implementation which uses NSPredicate as a generic matcher for greater flexibility.

@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

Usage:

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


i Got solution for this just use it where you want to remove all sessions

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

it will work

0

精彩评论

暂无评论...
验证码 换一张
取 消