Reputation: 8735
I'm trying to perform a query by multiple values. Unfortunately I can't use the ANY operator because I've to perform a CONTAINS operation.
Exemple:
Obj *foo1 = [Obj objectWithName:@"John" age:42];
Obj *foo2 = [Obj objectWithName:@"Elizabeth" age:21];
Obj *foo3 = [Obj objectWithName:@"Albert" age:17];
Obj *foo4 = [Obj objectWithName:@"Marty" age:54];
NSArray *query = @[@"Joh", @"lizabe", @"arty"];
// This query should return foo1
, foo2
and foo4
.
I tried something like but it doesn't work :
[NSPredicate predicateWithFormat:@"(name CONTAINS[cd]) IN %@, query];
Any suggestion ?
Upvotes: 1
Views: 180
Reputation: 1643
NSMutableArray *filterArray = [NSMutableArray array];
NSArray *query = @[@"Joh", @"lizabe", @"arty"];
for (NSString *string in query)
{
[filterArray addObjectsFromArray:[mainArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name CONTAINS[cd] IN %@", string]]];
}
Here mainarray is your objects containing array.
or try this..
NSMutableArray *filterArray = [NSMutableArray array];
NSMutableArray *predicateArray = [NSMutableArray array];
NSArray *query = @[@"Joh", @"lizabe", @"arty"];
for (NSString *string in query)
[predicateArray addObject:[NSPredicate predicateWithFormat:@"name CONTAINS[c] %@", string]];
NSPredicate *compoundpred = [NSCompoundPredicate andPredicateWithSubpredicates:predicateArray];
[filterArray addObjectsFromArray:[mainArray filteredArrayUsingPredicate:compoundpred];
I hope this will help for you...
Upvotes: 1