我有
NSDictionary *myDictionary = @{ @"1":@"Egg Benedict",
@"2":@"Mushroom Risotto",
@"3":@"Full Breakfast",
@"4":@"Hamburger",
@"5":@"Ham and Egg Sandwich",
@"6":@"Cream Brelee"};
NSMutableArray *myKeys = [NSMutableArray arrayWithArray:[myDictionary allKeys]];
我想用谓词过滤我的词典。我应该如何构建它?作为一种算法,我想它应该是这样的:
[filteredKeys removeAllObjects];
for (NSString *a in myKeys) {
if ([[myDictionary objectForKey:a] rangeOfString:searchText].location != NSNotFound) {
[filteredKeys addObject:a];
}
}
如何使用谓词字符串实现这一点?
您必须myObjects
为可变的键数组,因此您可以过滤该数组。键只是字符串。其谓词应该是:
@"SELF contains[c] %@", searchText
过滤数组后,您可以使用以下命令获取更新的字典:
NSDictionary *filtered = [myDictionary dictionaryWithValuesForKeys:myObjects];