2012-03-01 66 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); 
    }