从路径段数组创建NSDictionary

时间:2012-03-01 03:51:57

标签: ios nsarray nsdictionary

从扁平化路径的NSArray构建NSDictionary的最佳方法是什么?例如,我想转换此数组的内容:

<array>
<string>packs/</string>
<string>packs/Children/</string>
<string>packs/Children/Letters</string>
<string>packs/Children/Letters/abc.pack</string>
<string>packs/Children/Numbers</string>
<string>packs/Children/Numbers/123.pack</string>                                    
<string>packs/Children/Numbers/10_2_30.pack</string>
<string>packs/General/</string>
</array>

...进入路径段和文件名的NSDictionary,如下所示:

packs/
    Children/
    Letters/
        abc.pack
    Numbers/
        123.pack
        10_20_30.pack
    General/

首先查找带有文件扩展名(.pack)的数组项并从该点开始构建结构是否最好?或者尝试在数组的内容中逐行构建结构?

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

为简单起见,我假设所有叶节点都以.pack结尾,而所有分支节点都没有。

字典是一组键/值对。我们不清楚您希望密钥abc.pack的值在packs/Letters字典中。我只是使用字符串@"leaf node!"作为值。

使用将路径插入字典树的辅助函数,您可以非常轻松地完成此操作。

void insertPathIntoTree(NSString *path, NSMutableDictionary *tree) {
    NSArray *components = [path pathComponents];
    for (int i = 0, count = components.count; i < count; ++i) {
        NSString *component = [components objectAtIndex:i];

        if (!component.length) {
            // This ignores a trailing slash, and any double slashes mid-path.
            continue;
        }

        if (i == count - 1 && [component hasSuffix:@".pack"]) {
            [tree setObject:@"leaf node!" forKey:component];
        }

        else {
            NSMutableDictionary *nextBranch = [tree objectForKey:component];
            if (!nextBranch) {
                nextBranch = [NSMutableDictionary dictionary];
                [tree setObject:nextBranch forKey:component];
            }
            tree = nextBranch;
        }
    }
}

然后,只需要创建一个初始的空树(NSMutableDictionary)并将每个路径插入其中:

NSMutableDictionary *treeWithPathArray(NSArray *paths) {
    NSMutableDictionary *tree = [NSMutableDictionary dictionary];
    for (NSString *path in paths)
        insertPathIntoTree(path, tree);
    return tree;
}

答案 1 :(得分:0)

最好是通过查看扩展来构建结构的第一个。

<强>更新 这是一个简单的例子

NSArray *arrayPaths = [NSArray arrayWithObjects:@"packs/", @"packs/Children/", @"packs/Children/Letters", @"packs/Children/Letters/abc.pack",  @"packs/Children/Numbers", @"packs/Children/Numbers/123.pack", @"packs/Children/Numbers/10_2_30.pack", @"packs/General/", nil];

    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    for (NSString *filePath in arrayPaths) {
        NSString *fileExtention = [filePath pathExtension];
        if (![fileExtention isEqualToString:@""]) {
            NSArray *pathComponents = [filePath pathComponents];
            NSMutableDictionary *tDict = nil;
            NSMutableDictionary *lastDict = dictionary;
            for (int i = 0; i < [pathComponents count] - 1; i++) {
                if (i == ([pathComponents count] - 2)) {
                    NSString *key = [pathComponents objectAtIndex:i];
                    NSMutableArray *array = [lastDict objectForKey:key];
                    if (array == nil) {
                        array = [NSMutableArray array];
                    }
                    [array addObject:[pathComponents lastObject]];
                    [tDict setObject:array forKey:key];
                    break;
                }
                NSString *key = [pathComponents objectAtIndex:i];
                tDict = [lastDict objectForKey:key];
                if (tDict == nil) {
                    tDict = [NSMutableDictionary dictionary];
                }
                [lastDict setObject:tDict forKey:key];
                lastDict = tDict;
            }
        }
        NSLog(@"%@",dictionary);
    }