If I have two string objects that both have value "hi", and I add them to an NSMutableSet, are they treated as different objects or not? Thanks a bunch!
Asked
Active
Viewed 455 times
-3
-
5Did you try it first? What were the results? – rmaddy May 29 '15 at 17:10
-
1Well, it doesn't duplicate them. But they are different objects, right...? – billy bob May 29 '15 at 17:17
-
1But they are both equal. `NSSet` only keeps objects with different values based in the `isEqual:` method of the object. – rmaddy May 29 '15 at 17:18
1 Answers
2
Beside the correct comment of @rmaddy, there is a fundamental problem with it: It is simply mostly impossible to guarantee that no objects are equal in a set. (It is easy to guarantee that the are not identical.) This would imply to recheck the equaty of all objects (expensive), when one changes (mostly impossible to detect). This is, because NSSet
does not copy its content objects.
Let's have an example:
NSMutableString *first = [@"Amin" mutableCopy];
NSMutableString *second = [@"Amin Negm" mutableCopy];
NSSet *set = [NSSet setWithObjects:first, second];
[first appendString:@" Negm"];
Both objects are equal than, but none is removed. (Which one?)

Amin Negm-Awad
- 16,582
- 3
- 35
- 50