I am struggling to figure if whether adding an already existing object to a set in Cocoa actually replaces the object or simply ignores addObject: if there is a duplicate. I am using a custom object that is considered the same as another object if a specific field is equal.
I am overriding both isEqual: and hash methods and containsObject: does return true when I call it, but I would like to update the set with the new object and for some reason it doesn’t work if I call addObject:.
If it does ignore it, what would be the best data structure to use in place of NSMutableSet in order to have the desired effect?
NSMutableSet‘s-addObject:method will not add an object which passes the-member:test, as such it doesn’t do anything.If you still want to update the set with the new object regardless, you can use an
NSMutableArray, on which you’d call-replaceObjectAtIndex:withObject::