Ios 从路径段数组创建NSDictionary
从一系列平坦路径构建NSDictionary的最佳方法是什么?例如,我想转换此数组的内容:Ios 从路径段数组创建NSDictionary,ios,nsarray,nsdictionary,Ios,Nsarray,Nsdictionary,从一系列平坦路径构建NSDictionary的最佳方法是什么?例如,我想转换此数组的内容: <array> <string>packs/</string> <string>packs/Children/</string> <string>packs/Children/Letters</string> <string>packs/Children/Letters/abc.pack</string&
<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>
是否最好先查找具有文件扩展名(.pack)的数组项,然后从该点重新构建结构?或者尝试通过数组的内容逐行构建结构
非常感谢您的帮助 最好是第一个通过查看扩展来构建结构 已更新 这是一个简单的例子
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);
}
NSArray*arrayPath=[NSArray数组,其对象为:@“packs/”、@“packs/Children/”、@“packs/Children/Letters/abc.pack”、@“packs/Children/Numbers/123.pack”、@“packs/Children/Numbers/10_2_30.pack”、@“packs/General/”、nil];
NSMutableDictionary*dictionary=[[NSMutableDictionary alloc]init];
for(NSString*ArrayPath中的文件路径){
NSString*FileExtension=[filePath pathExtension];
如果(![FileExtension IsequalString:@“”){
NSArray*pathComponents=[filePath pathComponents];
NSMutableDictionary*tDict=nil;
NSMutableDictionary*lastDict=字典;
对于(int i=0;i<[pathComponents计数]-1;i++){
如果(i==([pathComponents计数]-2)){
NSString*key=[pathComponents对象索引:i];
NSMutableArray*数组=[lastDict objectForKey:key];
if(数组==nil){
array=[NSMutableArray];
}
[array addObject:[pathComponents lastObject]];
[tDict setObject:array forKey:key];
打破
}
NSString*key=[pathComponents对象索引:i];
tDict=[lastDict objectForKey:key];
如果(tDict==nil){
tDict=[NSMutableDictionary];
}
[lastDict setObject:tDict forKey:key];
lastDict=tDict;
}
}
NSLog(@“%@”,字典);
}
为了简单起见,我假设所有叶节点都以.pack
结尾,而所有分支节点都不是
字典是一组键/值对。不清楚您希望键abc.pack
的值在packs/Letters
字典中是什么。我将使用字符串@“叶节点!”
作为值
使用一个将路径插入字典树的助手函数,您可以非常轻松地完成这项工作
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;
}
}
}
您的输入数据不一致。某些分支节点(例如
packs/
和packs/Children/
)以斜杠结尾,而其他分支节点(例如packs/Children/Letters
和packs/Children/Numbers
)则不以斜杠结尾。这是您想要支持的内容,还是那些输入错误?或者规则很简单:如果节点以.pack
结尾,则它是一个叶节点。否则它就是一个分支节点。请澄清。
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 *treeWithPathArray(NSArray *paths) {
NSMutableDictionary *tree = [NSMutableDictionary dictionary];
for (NSString *path in paths)
insertPathIntoTree(path, tree);
return tree;
}