2012-03-01 47 views
0

从平坦路径的NSArray构建NSDictionary的最佳方法是什么?例如,我想这个数组的内容转换:从路径段数组创建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)的数组项目并从该点开始构建结构?或者尝试通过数组的内容逐行构建结构?

任何帮助,非常感谢!

+0

。在你输入的数据不一致。一些分支节点(例如'packs /'和'packs/Children /')以斜杠结尾,其他分支节点(例如'packs/children/letters'和'packs/Children/Numbers')不会。这是你想要支持的东西,还是那些错别字? – 2012-03-01 04:45:02

+0

或者规则很简单:如果节点以'.pack'结尾,则它是一个叶节点。否则它是一个分支节点。请澄清。 – 2012-03-01 04:46:09

回答

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; 
} 
+0

谢谢,这个作品真的很好! – Daniel 2012-03-01 13:58:22

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); 
    }