为了测试餐馆搜索应用程序,我在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);
}];
}