Search code examples
objective-ccocoa-touchnsset

Checking for and then deleting an object in an NSSet


I have an NSSet that contains four NSStrings. If I call -containsObject: to see if the NSSet contains a certain string, is it possible to delete the entry in the NSSet that is equal to the string that I just checked?

If my question makes no sense, it may be because using a NSSet may be the wrong way to approach this.

I have four NSStrings and would like to put them in some sort of group (Set, Array, etc.). I then want to check if another NSString matches any of the NSStrings in the group. If there is a match between the NSString and a NSString inside the group, I want to be able to delete the NSString match inside the group. Is this possible to do?


Solution

  • Let's say your set is *mySet and the object you want to remove is objectToRemove. Then you can do this:

    NSMutableSet *mutableSet = [NSMutableSet setWithSet:mySet];
    [mutableSet removeObject:objectToRemove];
    mySet = mutableSet;