假设下面的示例阵列:字符串分割到多维的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中执行此操作的方法。
为了澄清,更多信息会在每个字典否则我会使用数组。子文件夹将放入包含每个孩子的键/对象的字典中。 – amcc 2012-08-15 01:13:56
另一种结构是'Array {FolderDictionary,FolderDictionary,...}',其中每个'folderDictionary'都具有包含相等数组的'name','fullPath'和'childFolders'键 - 这可能更简单并且我试图启动用。 – amcc 2012-08-15 01:16:19
但我认为字典键更容易检查唯一性。我现在会停止评论! – amcc 2012-08-15 01:38:20