98

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.

Kazunori Takaishi
  • 2,268
  • 1
  • 15
  • 27
Slee
  • 27,498
  • 52
  • 145
  • 243

2 Answers2

240

What about NOT (CategoryID IN %@)?

dreamlax
  • 93,976
  • 29
  • 161
  • 209
3

How about using NONE?

[NSPredicate predicateWithFormat:@"NONE CategoryID IN %@", parentIDs];
Mark Adams
  • 30,776
  • 11
  • 77
  • 77
  • 4
    Note that NONE is only for to-many relationship. See http://stackoverflow.com/questions/12127618/nspredicate-the-left-hand-side-for-an-all-or-any-operator-must-be-either-an-nsa – Lubbo Jun 21 '13 at 11:06