Я предполагаю, что все конечные узлы оканчиваются на .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;
}