Using NSPredicate to search an array with an array

quantum picture quantum · Jul 2, 2012 · Viewed 22.2k times · Source

I have an array of Card objects (NSObjects), each with a field called tags, which is an NSArray of NSStrings.

I would then like to split up the user's search term into an array called keywords of strings by componentsSeparatedByString, then use NSPredicate to filter my array of Cards based on which elements have tags containing at least 1 keyword in keywords.

I hope that's not too convoluted! I've tried using the NSPredicate IN clause to no avail. How should I do this?

Answer

Vignesh picture Vignesh · Jul 2, 2012

Considering array contains card Object.

 NSArray *keyWordsList = [keywords componentSeparatedByString:@","];
 [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ANY %K IN %@",@"tags",keyWordsList]]

EDIT:

To search partially you can use LIKE operator.

[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ANY %K LIKE[cd] %@",@"tags",[partialkeyWord stringByAppendingString:@"*"]]]