Do NOT add or remove objects from any collection while enumerating it.
Create a copy and enumerate theat copy and manipulate the original. When finished dispose the copy.
Assuming you ARC a small change will do the trick.
for (CCNode *node in [self.contained allObjects]) { // for contained being an NSSet
if (!node.body || ![self validate:node]) {
[self.contained removeObject:node];
}
}
for (CCNode *node in [NSArray arrayWithArray:self.contained]) { // for contained being an NSArray
if (!node.body || ![self validate:node]) {
[self.contained removeObject:node];
}
}
for (CCNode *node in [NSDictionary dictionaryWithDictionary:self.contained]) { // for contained being an NSDictionary. However, allObjects or allKeys may suit you well too depending on what you need.
if (!node.body || ![self validate:node]) {
[self.contained removeObject:node];
}
}
If you don't ARC then add a call to the autorelease method on the newly created unnamed collection object. Like for (CCNode *node in [[self.contained allObjects] autorelease])
If you prefer an explicit enumerator then do:
NSEnumerator *e = [[self.contained allObjects] objectEnumerator];
while (CCNode *node = [e nextObject]) {