To test out a restaurant searching app I included 4 test restaurants in a JSON file which populate a table view correctly with their corresponding properties. In 3 text fields I filter the array with a name, average entree price and rating, yet when I pass it to the method the filtered array logged is not filtered. I don't see anything wrong with my predicate code though, any ideas? Thank you!
- (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);
}
The method the button calls
- (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);
}];
}