0

假设下面的示例阵列:字符串分割到多维的NSMutableDictionary

{"/documents", "/documents/files", "/pictures"} 

我想创建一个多维的NSMutableDictionary,看起来像(如果我是手动创建它):

NSArray *keys = [NSArray arrayWithObjects: @"documents", @"pictures", nil]; 
NSArray *objects = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObject:[NSDictionary dictionary] forKey:@"files"], [NSDictionary dictionary], nil]; 

NSMutableDictionary *demoDict = [NSMutableDictionary dictionaryWithObjects:objects forKeys:keys]; 

NSLog(@"%@", demoDict); 

哪会日志为:

documents = { 
    files = { 
    }; 
}; 
pictures = { 
}; 

我怎么可能从类似的阵列自动生成一个无限长度的路径长度(所以字典的无限维度?)

我到目前为止(希望它是有用的作为一个出发点)是; 我把上面的代码逻辑的意见,使其更容易对眼睛: (_folderPaths是数组)

/** 
*set the root dictionary 
*iterate through the array 
*Split the path down by the separator 
*iterate over the path parts 
*make sure there is a part to the part, eliminates initial slash or 
    double slashes 
*Check if key exists 
*if not then set a new mutdict for future children with key being the pathpart 
**/ 

NSMutableDictionary *foldersDictionary = [NSMutableDictionary dictionary]; 


for(NSString *path in _folderPaths){ 

    NSArray *pathParts = [path componentsSeparatedByString:@"/"]; 

    for(NSString *pathPart in pathParts){ 

     if([pathPart length]>0) 
     { 
      if(![foldersDictionary objectForKey:pathPart]) 
       [foldersDictionary setObject:[NSMutableDictionary dictionary] forKey:pathPart]; 
      //Some way to set the new root to reference the Dictionary just created here so it can be easily added to on the next iteration? 
     } 

    } //end for pathPart in pathParts 
} //end for path in _folderPaths 

NSLog(@"%@", foldersDictionary); 

这将记录为:

documents = { 
}; 
files = { 
}; 
pictures = { 
}; 

所以我需要一种方法能够在分割路径的每次迭代中更深入地进入词典。我之前在C#中的节点视图中完成了此操作,我可以使用游标引用子项,但我没有找到使用指针在Objective-C中执行此操作的方法。

+0

为了澄清,更多信息会在每个字典否则我会使用数组。子文件夹将放入包含每个孩子的键/对象的字典中。 – amcc 2012-08-15 01:13:56

+0

另一种结构是'Array {FolderDictionary,FolderDictionary,...}',其中每个'folderDictionary'都具有包含相等数组的'name','fullPath'和'childFolders'键 - 这可能更简单并且我试图启动用。 – amcc 2012-08-15 01:16:19

+0

但我认为字典键更容易检查唯一性。我现在会停止评论! – amcc 2012-08-15 01:38:20

回答

1

你很近。你所需要做的就是动态地改变添加新词典的父代。你可以这样做是这样的:

NSMutableDictionary *folders = [NSMutableDictionary dictionary]; 

for (NSString *path in folderPaths) { 
    NSMutableArray *folderStack = [NSMutableArray arrayWithObject:folders]; 

    for (NSString *component in [path pathComponents]) { 
     if ([component isEqualToString:@"/"]) continue; 

     NSMutableDictionary *folder = [[folderStack lastObject] objectForKey:component]; 
     if (folder == nil) { 
      folder = [NSMutableDictionary dictionary]; 
      [[folderStack lastObject] setObject:folder forKey:component]; 
     } 
     [folderStack addObject:folder]; 
    } 
} 

注意,此方法下,这些阵列都会产生相同的结果:

{"/documents", "/documents/pictures", "/documents/pictures/favorites"} 
{"/documents/pictures/favorites", "/documents", "/documents/pictures"} 
{"/documents/pictures/favorites"} 
+0

辉煌,通过这样的另一个数组引用文件夹字典是我心中所想,但无法绕过心理障碍!正在分裂的道路上,比预先确定应该分裂的地方更有效率吗?我想分离的字符串方法在现实中会做同样的事情。 – amcc 2012-08-15 02:02:10