NSArray with NSPredicate using NOT IN

IosObjective CNsarrayNspredicate

Ios Problem Overview


I have an NSArray that I want to filter out certain objects using an NSPredicate, I was hoping I could use NOT IN since I saw that I can easily do an IN.

So I have my array:

self.categoriesList

Then I get the values I want to remove:

NSArray *parentIDs = [self.cateoriesList valueForKeyPath:@"@distinctUnionOfObjects.ParentCategoryID"];

This gives me a list of ParentCategoryID's for categories I DO NOT want to display, so I figure I can use an NSPredicate to remove them:

self.cateoriesList = [self.cateoriesList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"CategoryID NOT IN %@",parentIDs]];

This fails:

reason: 'Unable to parse the format string "CategoryID NOT IN %@"'

If I wanted to use just IN that works perfectly of course.

Ios Solutions


Solution 1 - Ios

What about NOT (CategoryID IN %@)?

Solution 2 - Ios

How about using NONE?

[NSPredicate predicateWithFormat:@"NONE CategoryID IN %@", parentIDs];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSleeView Question on Stackoverflow
Solution 1 - IosdreamlaxView Answer on Stackoverflow
Solution 2 - IosMark AdamsView Answer on Stackoverflow