gpt4 book ai didi

swift - 重写 NSTreeController 的排列对象

转载 作者:行者123 更新时间:2023-11-30 12:35:30 25 4
gpt4 key购买 nike

在我的项目中,NSTreeController 绑定(bind)到 NSOutlineView( cocoa 绑定(bind))。由于某种原因我无法使用NSPredicate从Core Data中过滤数据。因此,我尝试覆盖 NSTreeControllerarrangedObjects,但覆盖的 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)
}
}

我发现了

The value returned by NSTreeController's arrangedObjects method is opaque. You should only observe this property to determine that a change has occurred.

doc 。那么,NSOutineView 如何在不(直接)读取 arrangedObjects 的情况下使用它呢?

最佳答案

在 xib 或代码中设置 NSTreeControllerfetchPredicate

关于swift - 重写 NSTreeController 的排列对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42919957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com