2

My code looks like:

NSPredicate* pred = [NSPredicate predicateWithFormat:@"(title IN %@)", prev_set];
NSSet* remainingSet = [goal_set filteredSetUsingPredicate:pred ];

Where prev_set and goal_set are two different sets. "title" in a property in the objects contained in both sets.

My objective is so that all goals are rejected if they have previously been met (each goal having a unique title).

If I have a predicate like "title in %@" ALL of the objects in goal_set are rejected. If I use "NOT("title in %@)", then NONE of the objects in goal_set are rejected.

If I print out both sets, I see that they clearly have some (but not all) objects in common (that is, their title is the same).

Am I completely confused about the use of "IN" for NSPredicates? How would I accomplish my objective?

J.R.
  • 5,789
  • 11
  • 55
  • 78

2 Answers2

1

Try [NSPredicate predicateWithFormat:@"NOT (title IN %@.title)", prev_set].

Your predicates assume that prev_set contains NSString objects, but according to your description, it contains objects that just have a string property, so it will never contain the titles themselves. I hope that makes sense.

omz
  • 53,243
  • 5
  • 129
  • 141
0

By definition, removing the intersection of two sets A and B from A is the relative complement of B in A, also called set difference, i.e. A \ B.

Luckly NSMutableSet features the set operation that you need, namely minusSet:

NSMutableSet *mutableGoalSet = [goal_set mutableCopy];
[mutableGoalSet minusSet:prev_set];

Of course, for this to work, you will have to implement isEqual and hash methods of your goal objects in order to make them identifiable by their title. (relevant: Best practices for overriding isEqual: and hash)

Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • This is incorrect. The definition of `minusSet:` is `- (void)minusSet:(NSSet *)otherSet` and has void return type, so I cannot see how your code will work. – Echelon Nov 05 '13 at 08:34
  • @Echelon good catch, `minusSet:` acts on the set itself. I'm too used to pure collections, thank you for pointing it out. Fixed. – Gabriele Petronella Nov 05 '13 at 16:51