我有一个看起来像这样的 NSArray:
@[@"file1", @"directory/testfile", @"directory/otherdir/testfile", @"otherdir/", @"otherdir/otherfile"]
我正在尝试将其转换为 NSDictionary,如下所示:
@{
@"directory/":@{
@"otherdir/":@{
@"testfile":[..some array with data..]}
},
@"testfile":[..some array with data..]
},
@"otherdir/":@{
@"otherfile":[..some array with data..]
},
@"file1":[..some array with data..]
}
基本上,一个看起来像文件树的 NSDictionary。提前致谢!
编辑:我拥有的实际数组是这样的:
(
"blog/",
"blog/code.css",
"blog/style.css",
"etc/",
"etc/awsservices.png",
"etc/speeds.png",
"etc/test/",
"etc/test/other/",
"etc/test/other/speeds.png",
"site/",
"site/dino.png",
"site/error.css",
"site/main.css",
"site/signet.min.js"
)
我为此编写的代码:
-(NSDictionary*)buildDictionaryWithContentsOfPath:(NSMutableArray*)files {
NSString *subDir;
NSMutableDictionary *returnable = [[NSMutableDictionary alloc] initWithDictionary:@{}];
BOOL directory;
NSLog(@"%@", files);
for (S3ObjectSummary *objectSummary in files) {
if ([[objectSummary key] hasSuffix:@"/"]) {
if (subDir && [[objectSummary key] hasPrefix:subDir]) {
NSLog(@"prefixed %@", [objectSummary key]);
NSMutableDictionary *treeDict = [[NSMutableDictionary alloc] initWithDictionary:@{}];
[returnable[subDir] setObject:treeDict forKey:[objectSummary key]];
} else {
subDir = [objectSummary key];
NSLog(@"dir %@", [objectSummary key]);
NSMutableDictionary *treeDict = [[NSMutableDictionary alloc] initWithDictionary:@{}];
[returnable setObject:treeDict forKey:[objectSummary key]];
}
directory = true;
} else {
S3GetObjectMetadataRequest *getMetadataRequest = [[S3GetObjectMetadataRequest alloc] initWithKey:[objectSummary key] withBucket:self.bucket.name];
S3GetObjectMetadataResponse *getMetadataResponse = [self.client getObjectMetadata:getMetadataRequest];
if (![[objectSummary key] hasPrefix:subDir]) {
NSLog(@"file %@",[objectSummary key]);
[returnable setObject:@{@"filename":[objectSummary key], @"directory":@(directory), @"created_at":getMetadataResponse.lastModified} forKey:[objectSummary key]];
} else {
for (NSString __strong *pathComp in [[objectSummary key] pathComponents]) {
pathComp = [NSString stringWithFormat:@"%@/", pathComp];
NSLog(@"path component %@", pathComp);
if (![[objectSummary key] hasSuffix:@"/"]) {
if (returnable[pathComp]) {
NSLog(@"has comp %@", pathComp);
[returnable[pathComp] setObject:@{@"filename":[objectSummary key], @"directory":@(directory), @"created_at":getMetadataResponse.lastModified} forKey:[objectSummary key]];
}
}
}
}
directory = false;
}
//[self.fileTree addObject:@{@"filename":[objectSummary key], @"directory":@(directory), @"created_at":getMetadataResponse.lastModified}];
}
return returnable;
}
它返回的字典:
{
"blog/" = {
"blog/code.css" = {
"created_at" = "2013-12-07 12:13:37 +0000";
directory = 0;
filename = "blog/code.css";
};
"blog/style.css" = {
"created_at" = "2013-12-07 12:13:36 +0000";
directory = 0;
filename = "blog/style.css";
};
};
"etc/" = {
"etc/awsservices.png" = {
"created_at" = "2013-12-07 12:26:37 +0000";
directory = 0;
filename = "etc/awsservices.png";
};
"etc/speeds.png" = {
"created_at" = "2013-12-07 13:29:27 +0000";
directory = 0;
filename = "etc/speeds.png";
};
// PROBLEM HERE
"etc/test/" = {
};
"etc/test/other/" = {
};
"etc/test/other/speeds.png" = {
"created_at" = "2013-12-07 17:29:21 +0000";
directory = 0;
filename = "etc/test/other/speeds.png";
};
// PROBLEM HERE
};
"site/" = {
"site/dino.png" = {
"created_at" = "2013-12-07 12:13:59 +0000";
directory = 0;
filename = "pmerino/dino.png";
};
"site/error.css" = {
"created_at" = "2013-12-07 12:13:59 +0000";
directory = 0;
filename = "pmerino/error.css";
};
"site/main.css" = {
"created_at" = "2013-12-07 12:13:59 +0000";
directory = 0;
filename = "pmerino/main.css";
};
"site/signet.min.js" = {
"created_at" = "2013-12-07 12:13:59 +0000";
directory = 0;
filename = "pmerino/signet.min.js";
};
};
}
如您所见,这部分:
"etc/test/" = {
};
"etc/test/other/" = {
};
"etc/test/other/speeds.png" = {
"created_at" = "2013-12-07 17:29:21 +0000";
directory = 0;
filename = "etc/test/other/speeds.png";
};
应该是这样的:
"etc/" = {
"test/" = {
"other/" = {
"speeds.png" = {
"created_at" = "2013-12-07 17:29:21 +0000";
directory = 0;
filename = "etc/test/other/speeds.png";
};
};
};
};