Search code examples
objective-cmacoscocoanstreecontroller

NSTreeController: how do I find the parent of a node?


I'm looking for a similar method to: NSOutlineView parentForItem: for NSTreeController.

I need to know the parent of a node in the tree.


Solution

  • Nodes in an NSTreeController are instances of NSTreeNode, and NSTreeNode has a read-only parentNode property - so use that.

    The short snippet below gets the parent node of the first node in the tree-controller's selectedNodes array:

    let selectedNode = tree.selectedNodes.first! as! NSTreeNode
    let parentOfSelectedNode = selectedNode.parentNode!