Note that there are some explanatory texts on larger screens.

plurals
  1. POcontentsOfDirectoryAtPath with Contents of Subfolders objective-c
    primarykey
    data
    text
    <p>How can I get the contents of a directory and all of its subfolders? I would like to have tree stored in a <code>NSDictionary</code>.</p> <p>I want the dictionary to print something like this:</p> <pre><code>{ MyFolder = ( "Water.png", { MySubfolder = ( "Note.txt", { Sub-Subfolder = ( "3D.pdf", "MyFile.txt" ); } ); } ); } </code></pre> <h2>Ive tried:</h2> <pre><code>NSFileManager *manager = [NSFileManager defaultManager]; NSArray *array = [manager contentsOfDirectoryAtPath:path error:nil]; NSMutableDictionary *files = [[NSMutableDictionary alloc]init]; NSString *newPath = @""; for (int i=0; array.count&gt;i; i++) { newPath = [NSString stringWithFormat:@"%@/%@", path, [array objectAtIndex:i]]; //echo(newPath); if ([[[manager attributesOfItemAtPath:newPath error:nil] objectForKey:NSFileType] isEqualToString:NSFileTypeRegular]) { NSLog(@"Setting: %@||%@", [array objectAtIndex:i], [newPath lastPathComponent]); [files setObject:[array objectAtIndex:i] forKey:[newPath lastPathComponent]]; } else {echo([NSString stringWithFormat:@"newPath=%@", newPath]); dict = [self reachedDirectory:newPath dict:dict oldPath:path]; } } NSMutableDictionary *transferred = [dict objectForKey:[newPath lastPathComponent]]; if (!transferred) { transferred = [[NSMutableDictionary alloc]init]; } for (int n=0; files.count&gt;n; n++) { [transferred setObject:[[files allValues] objectAtIndex:n] forKey:[[files allKeys] objectAtIndex:n]]; } echo([newPath lastPathComponent]); [dict setObject:transferred forKey:[path lastPathComponent]]; return dict; </code></pre> <p>But All of the folders are not aligned and it doesnt go past the second dimension of subfolders. I would like it to be able to have as many subfolders that is possible. </p> <p>Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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