在我的项目中,NSTreeController绑定到NSOutlineView(可可绑定)。由于某些原因,我无法使用NSPredicate从核心数据中过滤数据。因此,我尝试覆盖arrangedObjectsNSTreeController,但是覆盖的arrangedObjects甚至没有被触发。 (我在其中设置了一个调试断点。)代码可以很好地编译,并且已经将实例更改为我的CleanTreeController。我只在邮件列表中找到an unanswered question asked a decade ago

// Subclass of NSTreeController
class CleanTreeController: NSTreeController {
    init() {
        // This gets called.
        super.init(content: nil)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override var arrangedObjects: Any {
        let oldArranged = super.arrangedObjects

        let selector = NSSelectorFromString("childNodes")

        guard let oldChildren = (oldArranged as! NSObject)
                            .perform(selector).takeUnretainedValue() as? [NSTreeNode]
        else
        {
            return oldArranged
        }

        let children = oldChildren.filter { (treeNode) in
            //... do some filtering
        }

        return TreeNodeProxy(with: children)
    }
}

// Homemade NSTreeNodeProxy
class TreeNodeProxy : NSObject {

    fileprivate var rootNode : NSTreeNode

    init(with children : [NSTreeNode]) {
        self.rootNode = NSTreeNode()
        self.rootNode.mutableChildren.addObjects(from: children)
    }

    @objc(childNodes)
    var children: [NSTreeNode]? {
        return rootNode.children
    }

    @objc(descendantNodeAtIndexPath:)
    func descendant(at indexPath: IndexPath) -> NSTreeNode? {
        return rootNode.descendant(at: indexPath)
    }
}


我发现了


  NSTreeController的splittedObjects方法返回的值是
  不透明。您应仅观察此属性来确定
  发生了变化。


doc中。那么NSOutineView如何在不读取(直接)的情况下消费arrangedObjects

最佳答案

在xib或代码中设置fetchPredicateNSTreeController

09-17 19:53