title: Objective-c recursion, advanced recursive method construction
body: <p>I want to mimic <code>NSDirectoryEnumerator</code> with a simple recursive function, but my attempts so far have had my loops stopping prematurely.</p>
  
  <p>The method needs to stop when there is no longer a directory or file present at an <em>n<sup>th</sup></em> level.</p>
  
  <p>How can I rearrange the method below to be a simple recursive function that will terminate accordingly?</p>
  
  <p>Any help would be much appreciated. Thanks.</p>
  
  <pre><code>#import "MenuFromPathData.h"
  
  @interface MenuFromPathData ()
  
  @property (nonatomic,strong) NSFileManager *myFileManager;
  
  @end
  
  @implementation MenuFromPathData
  
  @synthesize myFileManager=_myFileManager;
  
  #define ROOT_DIRECTORY @"TextData"
  
  //
  ////
  //
  
  - (void) simpleMenuArrayBuilder
  {
      NSArray *fileNamesLevel1 = [self fullArrayReturn:ROOT_DIRECTORY];
  
      for(int i = 0; i &lt; [fileNamesLevel1 count]; i++)
      {
          NSString *pathNameLevel2 = [NSString stringWithFormat:@"%@/%@",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i]];
          NSArray *fileNamesLevel2 = [self fullArrayReturn:pathNameLevel2];
  
          for(int j = 0; j &lt; [fileNamesLevel2 count]; j++)
          {
              NSString *pathNameLevel3 = [NSString stringWithFormat:@"%@/%@/%@",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j] ];
              NSArray *fileNamesLevel3 = [self fullArrayReturn:pathNameLevel3];
  
              for(int k = 0; k &lt; [fileNamesLevel3 count]; k++)
              {
                  NSString *pathNameLevel4 = [NSString stringWithFormat:@"%@/%@/%@/%@",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j],[fileNamesLevel3 objectAtIndex:k]  ];
                  NSArray *fileNamesLevel4 = [self fullArrayReturn:pathNameLevel4];
  
                  for(int l = 0; l &lt; [fileNamesLevel4 count]; l++)
                  {
                      NSString *pathNameLevel5 = [NSString stringWithFormat:@"%@/%@/%@/%@/%@",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j],[fileNamesLevel3 objectAtIndex:k],[fileNamesLevel4 objectAtIndex:l]  ];
                      NSArray *fileNamesLevel5 = [self fullArrayReturn:pathNameLevel5];
  
                      for(int m = 0; m &lt; [fileNamesLevel5 count]; m++)
                      {
                          NSString *pathNameLevel6 = [NSString stringWithFormat:@"%@/%@/%@/%@/%@/%@",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j],[fileNamesLevel3 objectAtIndex:k],[fileNamesLevel4 objectAtIndex:l], [fileNamesLevel5 objectAtIndex:m] ];
                          NSArray *fileNamesLevel6 = [self fullArrayReturn:pathNameLevel6];
                          NSLog(@"-- LVL 6.0 %@ -- ",pathNameLevel6);
  
                          for(int n = 0; n &lt; [fileNamesLevel6 count]; n++)
                          {
                              NSString *pathNameLevel7 = [NSString stringWithFormat:@"%@/%@/%@/%@/%@/%@%@",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j],[fileNamesLevel3 objectAtIndex:k],[fileNamesLevel4 objectAtIndex:l], [fileNamesLevel5 objectAtIndex:m],[fileNamesLevel6 objectAtIndex:n] ];
                              NSArray *fileNamesLevel7 = [self fullArrayReturn:pathNameLevel7];
                              NSLog(@"-- LVL 7.0 %@ -- ",pathNameLevel7);
  
  
                          }
                      }
                  }
              }
          }
      }
  }
  
  //
  ////
  //
  
  - (NSString*) resourcePath : (NSString*) pathName {
      return [[NSBundle mainBundle] pathForResource:pathName ofType:nil];
  }
  
  - (NSArray*) bundleArrayReturn : (NSString*)pathForResource {
      NSError* error;
      return [self.myFileManager contentsOfDirectoryAtPath:pathForResource error:&amp;error];
  }
  
  - (NSArray*) fullArrayReturn : (NSString*) pathName  {
      return [self bundleArrayReturn:[self resourcePath: pathName]];
  }
  
  //
  ////
  //
  
  - (NSFileManager*) myFileManager {
      if(!_myFileManager) {
          _myFileManager = [NSFileManager defaultManager];
      }
      return _myFileManager;
  }
  
  @end
  </code></pre>