通过嵌套的NSDictionary循环

时间:2013-04-11 03:29:48

标签: objective-c recursion plist

我知道这里有一个简单的解决方案,但我无法完全绕过它。 我有一个用户生成的.plist文件,其中可以有(合理的)无限量的嵌套树。我想要做的是根据plist的嵌套方式创建一个文件夹结构。

.plist example

由于我不知道主词典有多深,所以我无法弄清楚如何遍历它并告诉它创建一个目录然后深入到它并在其中创建目录。

我知道如何创建文件夹,它遍历整个列表是什么让我失望。我很确定我需要一个单独的递归方法,我不确定从哪里开始。键“儿童”和“名称”是我用来创建列表的。任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

我首先要定义要创建目录结构的目录的路径。然后调用该函数来构建目录结构。

NSString *pathToDirectory = @"./";
[self buildDirectoriesAtPath: pathToDirectory fromDictionary: dictionary];

函数 -buildDirectoriesAtPath:fromDictionary: 是递归的,看起来像这样:

- (void) buildDirectoriesAtPath: (NSString *) filepath fromDictionary: (NSDictionary *) dictionary {

    for (NSString *key in dictionary.allKeys) {

        if ([key isEqualToString: @"Children"]) {

            NSArray *children = [dictionary objectForKey: key];
            for (id object in children) {

                if ([object isKindOfClass: [NSDictionary class]]) {
                    NSDictionary *directoryInfo = (NSDictionary *) object;
                    NSString *directoryName = [directoryInfo objectForKey: @"Name"];
                    NSString *directoryPath = [NSString stringWithFormat: @"%@%@", filepath, directoryName];

                    //  Create directory
                    NSLog(@"Creating directory: %@", directoryPath);

                    //  Create subdirectories
                    NSArray *subdirectories = [directoryInfo objectForKey: @"Children"];
                    for (id directory in subdirectories) {

                        if ([directory isKindOfClass: [NSString class]]) {
                            NSString *subdirectoryName = (NSString *) directory;
                            NSString *subdirectoryPath = [NSString stringWithFormat: @"%@/%@", directoryPath, subdirectoryName];

                            //  Create directory
                            NSLog(@"Creating directory: %@", subdirectoryPath);
                        }
                        else if ([directory isKindOfClass: [NSDictionary class]]) {
                            NSDictionary *subdirectory = (NSDictionary *) directory;
                            NSString *subdirectoryName = [subdirectory objectForKey: @"Name"];
                            NSString *subdirectoryPath = [NSString stringWithFormat: @"%@/%@/", directoryPath, subdirectoryName];

                            [self buildDirectoriesAtPath: subdirectoryPath fromDictionary: (NSDictionary *) directory];
                        }
                    }
                }
                else if ([object isKindOfClass: [NSString class]]) {
                    NSString *directoryName = (NSString *) object;
                    NSString *directoryPath = [NSString stringWithFormat: @"%@%@", filepath, directoryName];

                    //  Create directory
                    NSLog(@"Creating directory: %@", directoryPath);
                }
            }
        }
    }
}

如果你有任何想澄清的内容,请不要犹豫。 :)

答案 1 :(得分:1)

可能这个功能对你有帮助,

void recursiveCall (NSDictionary *dictionary) {
    NSArray *_keys = [dictionary allKeys];
    for (NSString *_key in _keys) {
        id obj = [dictionary valueForKey:_key];
        NSLog(@"Create folder of %@",_key);
        if ([obj isKindOfClass:[NSArray class]]) {
            for (id _obj in obj) {
                if ([_obj isKindOfClass:[NSDictionary class]]) {
                    recursiveCall(_obj);
                }
                else {
                    NSLog(@"Create folder of %@",_obj);
                }
            }
        }

    }
}