In Smalltalk i am used to make a copy of a collection without an undesired element like this:
myCollection copyWithout: undesiredObject
which answers a copy of the receiver that does not contain any elements equal to undesiredObject
.
Is there an equivalent in Cocoa?
If not, what is the best way to achieve such a copy?
I am especially interested in copying instances of NSSet
.
There is no direct equivalent in the Cocoa Foundation classes.
Possible method #1:
NSSet *withoutUndesiredObject = [myCollection objectsPassingTest:^BOOL(id obj, BOOL *stop) {
return ![obj isEqualTo:undesiredObject];
}];
Possible method #2:
NSMutableSet *withoutUndesiredObject = [myCollection mutableCopy];
[withoutUndesiredObject removeObject:undesiredObject];
Similar methods exist also for the other collection classes NSArray
and NSDictionary
.