我正在尝试遍历我的所有NSUserDefaults并删除它们,问题是它们的数量有所变化。
有没有办法做这样的事情
for (NSUserDefaults that key starts with "highScoreXXX") {
*the XXX need to be wildcards*
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"key"];
}
答案 0 :(得分: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];
}