Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a NSDictionary with NSArray with file paths?
    text
    copied!<p>I have a NSArray which looks like this:</p> <pre><code>@[@"file1", @"directory/testfile", @"directory/otherdir/testfile", @"otherdir/", @"otherdir/otherfile"] </code></pre> <p>And I'm trying to convert it to a NSDictionary which looks like this:</p> <pre><code> @{ @"directory/":@{ @"otherdir/":@{ @"testfile":[..some array with data..]} }, @"testfile":[..some array with data..] }, @"otherdir/":@{ @"otherfile":[..some array with data..] }, @"file1":[..some array with data..] } </code></pre> <p>Basically, a NSDictionary which looks like a file tree. Thanks in advance!</p> <h3>EDIT: The actual array I have is this:</h3> <pre><code>( "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" ) </code></pre> <p>And the code I wrote for this:</p> <pre><code>-(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 &amp;&amp; [[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; } </code></pre> <p>And the dictionary it returns: </p> <pre><code>{ "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"; }; }; } </code></pre> <p>As you can see, this part:</p> <pre><code>"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"; }; </code></pre> <p>Should look like this:</p> <pre><code>"etc/" = { "test/" = { "other/" = { "speeds.png" = { "created_at" = "2013-12-07 17:29:21 +0000"; directory = 0; filename = "etc/test/other/speeds.png"; }; }; }; }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload