I’m trying to filter an NSArray by excluding the elements that are in an NSSet. I’m doing something like this:
NSMutableArray* a = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];
NSSet* set = [NSSet setWithObjects:@"2", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NONE SELF IN %@", set];
NSArray* b = [a filteredArrayUsingPredicate:predicate];
However, this code throws an exception:
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet
What is that I’m doing wrong?
You should use
NOTinstead ofNONEin the predicate:It is because the predicate is applied to each object (
SELF) in the array, which isNSString. On the contrary,Noneshould be applied on theNSArrayofNSSet.If you insist on using
NONE. You may changeatoso that the
SELFbecomesNSArray.