I’m trying to find a matching object in a tree, so I’m using ObjC fast enumeration. The problem is my method finds the matching value, hits the return line, and then sets the value to nil and keeps on iterating. Here’s my method:
+ (INONode *)findByUUID:(NSString*)uuid fromRootNode:(INONode*)node
{
for (INONode * childNode in [node children]) {
if ([[node uniqueID] isEqualToString:uuid]) {
break;
}
else {
[INONode findByUUID:uuid fromRootNode:childNode];
}
}
return node;
}
When I follow code execution by setting a breakpoint, the break is hit, then goes to the return line, then back up to the statement that continues the iteration. What am I missing here?
Since your method is recursive, the
returnis returning you to theelsebranch of theifin your loop and continuing the search.The way it is currently implemented, your method will only ever return the node passed in to it. The only
returnstatement isreturn node, andnodeis never modified.Here is one way you could do it: