NSTreeController display plist

Hello,

I need a quick and simple view to display a plist. I thought NSTreeController would do that nicely but I'm not seeing how to connect it. Does anyone have a quick example on how to do this?


Thanks!

Replies

A NSTreeController only works directly if the hierarchy of the plist is provided through arrays exclusively. If there are dictionaries providiing hierarchy, you're going to have to transform the plist into some form where the dictionary entries can be indexed via a numeric subscript.


A reasonable approach would be to walk the plist manually, and build a parallel hierarchy using NSTreeNode objects, where the represented object of each NSTreeNode instance is the corresponding object in the plist (arrays, dictionaries and sets being branch nodes, everything else being leaf nodes).


To use a NSTreeController for display, you can theoretically use a NSOutlineView and bind its content to the tree controller, whose own content is provided by the NSTreeNode tree, which represents the plist data. (There may be a way of doing it without the extra NSTreeNode tree, because NSTreeController constructs its own private tree of NSTreeNode objects anyway, but this is usually the point where my head starts to hurt.)


Using NSTreeController is a bit of a PITA, but if you read the documentation carefully, it likely not a lot of code. Or you may find some sample code if you search Apple's sample code or (say) GitHub.


Whether any of this comes under "quick and simple", I couldn't say. 🙂

Thanks, I dont mind writitng the code. To walk the plist. I honestly thought it would be pretty simple to just read in the data from the plist and have the NSTreeController just parse it for me :-). I know, lazy.


I'll take another look at the apple docs and take a quick stab at it.


Thanks again!

Unfortunately, a tree controller can't parse anything. From the NSTreeController class documentation:


The NSTreeController class provides selection and sort management. Its primary purpose is to act as the controller when binding NSOutlineView and NSBrowser instances to a hierarchical collection of objects. The root content object of the tree can be a single object, or an array of objects.


An NSTreeController object requires that you describe how the tree of objects is traversed by specifying the key-path for child objects specified by childrenKeyPath.


That is, every object in the tree must be accessible using the same key-path, applied to the object's parent branch node, along with a numerical index. That doesn't work for non-array collections.


Parsing the plist is easy — use property list serialization APIs — but that part doesn't involve tree controllers. A tree controller is solely a lens for seeing hierarchical data as a homogeneous tree.