使用多个条件过滤NSArray的NSDrary

时间:2013-11-12 14:25:51

标签: ios objective-c cocoa-touch nsarray nspredicate

我使用以下代码过滤我的数组

NSArray *_filteredArray = [self.allData filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    NSDictionary *_dataRow = (NSDictionary *)evaluatedObject;
    return ([[[_dataRow valueForKey:@"name"] lowercaseString]  rangeOfString:[self.searchBar.text lowercaseString]].location != NSNotFound);
}]];

正如您在代码段中看到的,我只匹配@"name";如何检查第二个参数(例如@"city")与我的过滤器匹配?

我尝试了allKeysallValues,但都没有效果。

1 个答案:

答案 0 :(得分:0)

做这样的事情:

NSArray *_filteredArray = [self.allData filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSDictionary *_dataRow = (NSDictionary *)evaluatedObject;
return ([[[_dataRow valueForKey:@"name"] lowercaseString]  rangeOfString:[self.searchBar.text lowercaseString]].location != NSNotFound) || ([[[_dataRow valueForKey:@"surname"] lowercaseString]  rangeOfString:[self.searchBar.text lowercaseString]].location != NSNotFound);
}]];