I'm not sure I understood your question correctly, I analyze it as: When implementing addChild:
how can I prevent objects from being _childs
second time in _childs
?
There are two ways: if the order of your elements does not matter, you should simply use NSMutableSet
instead of NSMutableArray
. In this case, the set takes care of everything:
- (void)addChild:(ChildClass *)child { [_childs addObject:child]; }
If order is important, stick with NSMutableArray
and do it like this:
- (void)addChild:(ChildClass *)child { if ([_childs containsObject:child]) return; [_childs addObject:child]; }
source share