Search code examples
swiftobjective-cnspredicate

Can NSPredicate Compare a Property to an Array of Wildcards?


I need an NSPredicate that compares a property to an array of wildcards. Something like...

NSArray *collection = @[@"Jerry*",@"George*",@"Elaine*"];
[NSPredicate predicateWithFormat:@"property LIKE IN %@", collection];

Is this possible?


Solution

  • I ended up using NSCompoundPredicate to get this done. I was hoping there was a shorter route but it's fairly painless.

    NSArray *collection = @[@"Jerry*",@"George*",@"Elaine*"];
    NSMutableArray *thePredicateArray = [NSMutableArray arrayWithCapacity:3];
            
    for (id *theItem in collection)
       {
       [thePredicateArray addObject:[NSPredicate predicateWithFormat:@"property LIKE %@",theItem]];
       }    
        
    NSCompoundPredicate *theCompoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:thePredicateArray];