How to use NSBrowserDelegate methods to represent data tree

166 views Asked by At

I am trying to represent a JSON dictionary in an NSBrowser on OS X 10.11. I have a dictionary which contains members of either string values, arrays of string values, or dictionaries of the same.

At func rootItemForBrowser(browser: NSBrowser) -> AnyObject? I provide my dictionary.

At func browser(browser: NSBrowser, numberOfChildrenOfItem item: AnyObject?) -> Int I return the count of objects in either a child array or dictionary.

I am getting confused with how to use func browser(browser: NSBrowser, child index: Int, ofItem item: AnyObject?) -> AnyObject. The return value here ends up being a string value of one of the members, which is a key to more data. But when I then select this child, I just have a parent string to work from. I need the entire path of what the user has chosen so far to actually achieve traversal of the dictionary.

I've tried looking at the path() method of NSBrowser but that causes an infinite loop in these delegate methods. I look at apps like Finder and am curious as to how they achieve path as I want.

1

There are 1 answers

0
Ricky On

I ended up coming to the conclusion that using a dictionary on its own with the browser delegate methods wasn't getting me anywhere. What worked for me, and what I recommend for others, is to create an object which represents the dictionary that can be passed into the browser.

For example:

class DictionaryItem {
    let name: String
    var children: [DictionaryItem]? 
    var rawData: AnyObject?

    init(name: String) {
        self.name = name
    }
}

Traverse the dictionary and add these objects to an array which the browser will use as its data source.