In my project an NSTreeController
is bound to an NSOutlineView
(cocoa binding). For some reason I can't use NSPredicate
to filter data from Core Data. So I tried to override arrangedObjects
of NSTreeController
, but the overriding arrangedObjects
has not even been fired . (I set a debugging breakpoint inside it.) The code compiles well and I've already changed the instance to my CleanTreeController
. I only found an unanswered question asked a decade ago in the mailing list.
// 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)
}
}
I've found
The value returned by NSTreeController's arrangedObjects method is opaque. You should only observe this property to determine that a change has occurred.
in the doc . So how can NSOutineView
consume arrangedObjects
without reading it (directly)?
设置fetchPredicate
的的NSTreeController
在厦门国际银行或代码。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.