HurkNburkS
HurkNburkS

Reputation: 5510

How to filter NSArray using two NSPredcates

I would like to filter an NSArray of NSDictionaries, however I would like to filter the result using one, two or even three NSPredicate values?

Currently I am filtering my array by doing this.

NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"parts == %@", filterString];//keySelected is NSString itself
        NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[currentParts filteredArrayUsingPredicate:predicateString]];
        sortedItemsArray = [filteredArray mutableCopy];

But I am not sure how I would do this using two predicates?

The other two predicates individually look like the one above accept different keys.

NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"area == %@", filterString];

and

NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"item == %@", filterString];

What I was thinking is that maybe you could have something like

NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"stage == %@ area == %@", filterString, areaflterstring];

But I don't think that's possible.

Upvotes: 0

Views: 96

Answers (1)

Wain
Wain

Reputation: 119021

It is possible, but you need to tell the predicate how to combine the parts, like:

NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"stage == %@ AND area == %@", filterString, areaflterstring];

You can alternatively use NSCompoundPredicate to combine a number of predicates.

Upvotes: 1

Related Questions