objective-c - 将字符串拆分为多维 NSMutableDictionary

标签 objective-c ios cocoa-touch nsdictionary 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# 中的节点 View 上完成过此操作,我可以在其中使用光标引用子项,但我没有找到使用指针在 Objective-C 中执行此操作的方法。

最佳答案

你已经很接近了。您需要做的就是动态更改新词典添加到的父级。你可以这样做:

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"}

关于objective-c - 将字符串拆分为多维 NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11962863/

相关文章:

objective-c - 参数的可变参数方法 : Check, 是一个 typedef ed block

ios - XMPP 主机名问题

ios - 如何在 React Native 中获取 contentOffset 属性的 ScrollView 组件的内容高度?

objective-c - 让Objective-C实现 block 返回类型

iphone - NSDate/NSDateFormatter - 只存储时间,不存储日期?

ios - 如何增加UIButton的选择区域?

ios - cgcontext 旋转矩形

ios - PDFKIT IOS 11 A4 尺寸保存 Swift

objective-c - UIView subview 中的 setNeedsLayout

iphone - 选择的 UIButton 不起作用