I'm trying to filter a UITableView's
data using a UISearchDisplayController
and NSCompoundPredicate
. I have a custom cell with 3 UILabels
that I want to all be filtered within the search, hence the NSCompoundPredicate
.
// Filter the array using NSPredicate(s)
NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"SELF.productName contains[c] %@", searchText];
NSPredicate *predicateManufacturer = [NSPredicate predicateWithFormat:@"SELF.productManufacturer contains[c] %@", searchText];
NSPredicate *predicateNumber = [NSPredicate predicateWithFormat:@"SELF.numberOfDocuments contains[c] %@",searchText];
// Add the predicates to the NSArray
NSArray *subPredicates = [[NSArray alloc] initWithObjects:predicateName, predicateManufacturer, predicateNumber, nil];
NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
However, when I do this, the compiler warns me:
Incompatible pointer types initializing 'NSCompoundPredicate *_strong' with an expression of type 'NSPredicate *'
Every example I've seen online does this exact same thing, so I'm confused. The NSCompoundPredicate orPredicateWithSubpredicates:
method takes an (NSArray *)
in the last parameter, so I'm REALLY confused.
What's wrong?
orPredicateWithSubpredicates:
is defined to return an NSPredicate*. You should be able to change your last line of code to:... and still have all of the compoundPredicates applied.
First of all, using "contains" is very slow, consider mayber "beginswith"? Second, what you want is:
Three, you could've just done something like:
Here's an useful method i created based on the answers above (which i thank very much!)
It allows to create an NSPredicate dynamically, by sending an array of filter items and a string which represents the search criteria.
In the original case, the search criteria changes, so it should be an array instead of a string. But it may be helpful anyway