为了测试一个餐厅搜索应用程序,我在一个 JSON 文件中包含了 4 个测试餐厅,这些餐厅使用其相应的属性正确填充了一个表格视图。在 3 个文本字段中,我使用名称、平均主菜价格和评级过滤数组,但是当我将它传递给方法时,记录的过滤数组未被过滤。不过,我看不出我的谓词代码有什么问题,有什么想法吗?谢谢!
- (void)searchRestaurantsWithName:(NSString *)name price:(int)price andRating:(int)rating completion:(void (^)(NSArray *restaurants))completion {
NSMutableArray *restaurantsToFilter = [[RestaurantController sharedInstance].restaurants mutableCopy];
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[c] %@", name];
NSPredicate *pricePredicate = [NSPredicate predicateWithFormat:@"price < %i", price];
NSPredicate *ratingPredicate = [NSPredicate predicateWithFormat:@"rating > %i", rating];
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[namePredicate, pricePredicate, ratingPredicate]];
NSArray *filteredArray = [restaurantsToFilter filteredArrayUsingPredicate:compoundPredicate];
completion(filteredArray);
}
按钮调用的方法
- (void)filterArray {
[self searchRestaurantsWithName:self.nameTextField.text price:[self.priceTextField.text intValue] andRating:[self.ratingTextField.text intValue] completion:^(NSArray *restaurants) {
[self.tableView reloadData];
NSLog(@"%@", restaurants);
}];
}