Following your depth numbering - i.e. starting with root at 1, not 0:
-(void)allNodesFromTree:(CFTreeRef)node currentDepth:(int)currDepth atDepth:(int)depth nodesFound:(NSMutableArray*)nodes
{
bool atTargetDepth = depth -1 == currDepth;
CFTreeRef curChild = CFTreeGetFirstChild(node);
for (; curChild; curChild = CFTreeGetNextSibling(curChild)) {
if(atTargetDepth) //stop recursion if target depth reached
{
[nodes addObject:(__bridge id)(curChild)];
}
else [self allNodesFromTree:curChild currentDepth:currDepth+1 atDepth:depth nodesFound:nodes];
}
}
To make things a bit cleaner, you might wrap it in a helper function:
-(NSMutableArray*)allNodesFromTree:(CFTreeRef)node atDepth:(int)depth
{
NSMutableArray *nodesArray = [[NSMutableArray alloc]init];
[self allNodesFromTree:node currentDepth:0 atDepth:depth nodesFound:nodesArray];
return nodesArray;
}
Then you can just call
NSMutableArray *nodes = [self allNodesFromTree:root atDepth:3];