0

我有

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

我想用谓词过滤 myDictionary 。我应该如何构建它?作为一种算法,我想它应该是这样的:

[filteredKeys removeAllObjects];
for (NSString *a in myKeys) {
    if ([[myDictionary objectForKey:a] rangeOfString:searchText].location != NSNotFound) {
        [filteredKeys addObject:a];
    }
}

我怎样才能用谓词字符串来实现呢?

4

1 回答 1

0

您有myObjects一个可变的键数组,因此您可以过滤该数组。键只是字符串。谓词应该是:

@"SELF contains[c] %@", searchText

过滤数组后,您可以使用以下方法获取更新的字典:

NSDictionary *filtered = [myDictionary dictionaryWithValuesForKeys:myObjects];
于 2013-10-22T12:21:33.963 回答