0

I have an Exit object. Every Exit has an array of Category objects.

Objects look like this :

Category: (id, name)

Exit: (id,name,number,...,NSArray(Categories),...)

I want to use NSPredicate to filter out exits by Category name they have in Category array, but I'm not sure how exactly to write it down. If I'd wanted to filter out Exits by Exit name, for example, I would do this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.exitName = %@", name];
NSArray *results = [exits filteredArrayUsingPredicate:predicate];

But I'm not sure how to get into the Categories array and search by Category Name.

Any suggestions ?

2
  • Try this, NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.categories.name CONTAINS[c] %@", name]; Commented Nov 3, 2015 at 10:03
  • categories is an NSArray and does not have a name property. Commented Nov 3, 2015 at 10:08

2 Answers 2

1

Try this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.exitName.category_name CONTAINS[c] %@", name];
NSArray *results = [exits filteredArrayUsingPredicate:predicate];
Sign up to request clarification or add additional context in comments.

2 Comments

SELF is an exit, category_name isn't a property of the name of exit.
In the predicateformat, SELF can be seen as an instance of Exit. Exit doesn't have a property exitName, the property is name. This name property doesn't have a property category_name. This predicate won't work.
0

Try this predicate

NSPredicate *matchingCategory = [NSPredicate predicateWithFormat:@"SUBQUERY(categories, $c, name CONTAINS[cd] %@).@count > 0", categoryName];

Explanation

The SUBQUERY() function creates an array of objects which match the given sub-predicate. In this case, that sub-predicate is name CONTAINS[cd] %@. The way it works is by iterating over the collection (first param), assigning the name $c (second param) to refer to each element and then testing to see if the sub-predicate matches that element. If it matches, it's added to the array.

After the SUBQUERY() completes, the array is aggregated using the @count property. If there were any matches (@count > 0), the overall predicate on the exits will match the exit being tested.

5 Comments

A subquery is too complicated.
Also, filtering all categories and then checking the count is inefficient.
A solution which works is too complicated, but you don't have any alternative? What's the point of your comment, exactly? And the predicate is turned into SQL, which runs quite efficiently. Why do you think otherwise?
See my answer for an alternative. SQL isn't mentioned in the question.
I thought I read core data somewhere in the question. I apologize for that. Your answer has the unfortunate problem of not actually working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.