NSOutlineView 展开孩子

NSOutlineView expand childs

我有一个 NSOutlineView,我正在尝试实现 "search & replace" 功能,但问题是所有节点都没有扩展。

let nodes : [NSTreeNode] = self.getNode(contains: "any word")

for node in nodes {
  self.outlineView.expandItem(node.parent) // that only work for short index path

  let row = self.outline.row(forItem: item)
    if row >= 0 {
      self.outlineView.scrollRowToVisible(row)
      self.outlineView.selectRowIndexes(IndexSet(integer: row), byExtendingSelection: false)
    }
}

问题是某些级别的节点没有展开。
我如何开始扩展 NSTreenode(s) 从初始 parent 节点到包含我的搜索的最后一个 child parent?

递归,先展开父节点再展开节点:

func expandNode(_ node:NSTreeNode) {
    if let parent = node.parent {
        if !outlineView.isItemExpanded(parent) {
            self.expandNode(parent)
        }
    }
    outlineView.expandItem(node)
}

self.expandNode(node)

遍历 node.indexPath:

var treeNode = treeController.arrangedObjects
node.indexPath.forEach { index in
    treeNode = treeNode.children![index]
    if !outlineView.isItemExpanded(treeNode) {
        outlineView.expandItem(treeNode)
    }
}