Objective-c递归,高级递归方法构造

时间:2014-07-18 10:26:03

标签: objective-c recursion

我想用简单的递归函数模仿NSDirectoryEnumerator,但到目前为止我的尝试已经让我的循环过早停止了。

n th 级别不再存在目录或文件时,该方法需要停止。

如何将下面的方法重新排列为一个简单的递归函数,它将相应地终止?

非常感谢任何帮助。感谢。

#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 < [fileNamesLevel1 count]; i++)
    {
        NSString *pathNameLevel2 = [NSString stringWithFormat:@"%@/%@",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i]];
        NSArray *fileNamesLevel2 = [self fullArrayReturn:pathNameLevel2];

        for(int j = 0; j < [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 < [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 < [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 < [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 < [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:&error];
}

- (NSArray*) fullArrayReturn : (NSString*) pathName  {
    return [self bundleArrayReturn:[self resourcePath: pathName]];
}

//
////
//

- (NSFileManager*) myFileManager {
    if(!_myFileManager) {
        _myFileManager = [NSFileManager defaultManager];
    }
    return _myFileManager;
}

@end

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效。唯一的技巧是在方法中添加另一个参数来跟踪递归期间的当前深度。

- (void) simpleMenuArrayBuilderForPath:(NSString*)pathToDecend allowedDepth:(int)depth
{
    // if we've defended far enough, then stop
    if(depth == 0) return;

    // otherwise, get the next directory listing at this level
    NSArray *fileNamesNextLevel = [self fullArrayReturn:pathToDecend];
    for(int i = 0; i < [fileNamesNextLevel count]; i++)
    {
        // find each path inside the directory we're looking at
        NSString *nextLevelPathName = [pathToDecend stringByAppendingPathComponent:[fileNamesNextLevel objectAtIndex:i]];
        // process it however you want...
        NSLog(@"Looking at %@", nextLevelPathName);
        // and recur into it
        [self simpleMenuArrayBuilderForPath:nextLevelPathName allowedDepth:depth-1];
    }
}

然后,通过以下方式调用它:

[self simpleMenuArrayBuilderForPath:ROOT_DIRECTORY allowedDepth:5];