Padin215
Padin215

Reputation: 7484

NSPredicate and NSArray

Is there a way to setup a NSPredicate that will search for all items in an NSArray?

something like:

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"group.name == %@", arrayOfNames];

Upvotes: 0

Views: 80

Answers (2)

Adnan Aftab
Adnan Aftab

Reputation: 14477

Yes you can use NSPredicate with NSArray like this

NSArray *data = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"foo" forKey:@"BAR"]];    
NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(BAR == %@)", @"foo"]];

Upvotes: 0

Martin R
Martin R

Reputation: 539725

Use "IN" instead of "==" if the right-hand side is an array or set:

[NSPredicate predicateWithFormat: @"group.name IN %@", arrayOfNames]

Upvotes: 3

Related Questions