Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSArray with NSPredicate using NOT IN

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.

like image 603
Slee Avatar asked Dec 20 '11 19:12

Slee


People also ask

Can NSArray contain nil?

arrays can't contain nil.

How do you declare NSArray in Objective-C?

In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method. id objects[] = { someObject, @"Hello, World!", @42 }; NSUInteger count = sizeof(objects) / sizeof(id); NSArray *array = [NSArray arrayWithObjects:objects count:count];

How to use predicates in NSArray and nsmutable?

NSDictionary can use predicates by filtering its keys or values (both NSArray objects). NSOrdered Set can either create new ordered sets from a filtered NSArray or NSSet, or alternatively, NSMutable Set can simply remove Objects In Array:, passing objects filtered with the negated predicate.

How do you use predicates in NSDictionary?

NSDictionary can use predicates by filtering its keys or values (both NSArray objects). NSOrderedSet can either create new ordered sets from a filtered NSArray or NSSet, or alternatively, NSMutableSet can simply removeObjectsInArray:, passing objects filtered with the negated predicate.

How do I override primitive instance methods of an NSArray?

Any subclass of NSArray must override the primitive instance methods count and object (at:). These methods must operate on the backing store that you provide for the elements of the collection. For this backing store you can use a static array, a standard NSArray object, or some other data type or mechanism.

How do I access NSArray values?

In addition to the provided instance methods, such as object (at:), you can access NSArray values by their indexes using subscripting. There is typically little reason to subclass NSArray. The class does well what it is designed to do—maintain an ordered collection of objects.


2 Answers

What about NOT (CategoryID IN %@)?

like image 189
dreamlax Avatar answered Sep 24 '22 10:09

dreamlax


How about using NONE?

[NSPredicate predicateWithFormat:@"NONE CategoryID IN %@", parentIDs]; 
like image 43
Mark Adams Avatar answered Sep 25 '22 10:09

Mark Adams