V-JAY
V-JAY

Reputation: 81

possible to use wildcards?

I'm trying to loop through all my NSUserDefaults and remove them the problem is there is a changing number of them.

Is there a way to do something like this

for (NSUserDefaults that key starts with "highScoreXXX") {

     *the XXX need to be wildcards*

     [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"key"];

}

Upvotes: 3

Views: 486

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163288

NSUserDefaults has a method called -dictionaryRepresentation that you can use like so:

NSDictionary *defaultsDict = [[NSUserDefaults sharedUserDefaults] dictionaryRepresentation];
NSArray *keys = [[defaultsDict allKeys] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", @"highScore"]];

for(NSString *key in keys) {
   [[NSUserDefaults sharedUserDefaults] removeObjectForKey:key];
}

Upvotes: 3

Related Questions