shedri1 shedri1 - 11 months ago 57
Objective-C Question


I'm trying to filter a

data using a
. I have a custom cell with 3
that I want to all be filtered within the search, hence the

// 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?

Answer Source

First of all, using "contains" is very slow, consider mayber "beginswith"? Second, what you want is:

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];

Three, you could've just done something like:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.productName beginswith[cd] %@ OR SELF.productManufacturer contains[cd] %@", searchText, searchText];