Search code examples
objective-ccocoansfilemanagernsoutlineview

Using NSOutlineView as a file browser, starting from a given directory


I've been following this tutorial for using NSOutlineView as a hierarchical file browser:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/OutlineView/Articles/UsingOutlineDataSource.html

I used all the code in the tutorial and it worked. However, I then tried to call initWithPath: with a path other than / and it doesn't work: the fullPath of the top item (i.e. the folder specified in initWithPath) is just the name of the folder, and the children method of the FileSystemItem returns an empty array, I assume because the file manager is looking in /FolderName/ rather than the absolute path, which never appears to be saved.

How could I modify this code to allow it to do this?


Solution

  • The code above almost works. As soon as you try to open a folder it crashes. Try it with this modification. It works perfectly for me:

    - (id)initWithPath:(NSString *)path parent:(FileSystemItem *)parentItem {
      if (self = [super init]) {
        relativePath = [path copy];
        parent = parentItem;
      }
     return self;
    }