2011-10-04 60 views
3

我想通過我所有的NSUserDefaults循環,並刪除它們的問題是有不斷變化的數量。可能使用通配符?

有沒有辦法做這樣的事情

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

    *the XXX need to be wildcards* 

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

} 

回答

3

NSUserDefaults呼籲-dictionaryRepresentation的方法,您可以使用像這樣:

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