[英]Error with NSTreeController - this class is not key value coding-compliant for the key
I am new to Swift and trying to learn how to implement NSTreeController with NSOutlineView.我是 Swift 的新手,正在尝试学习如何使用 NSOutlineView 实现 NSTreeController。 I've been following several guides which shows such examples, but I keep getting an error.
我一直在遵循几个显示此类示例的指南,但我不断收到错误消息。 I followed step by step and/or try to run their source codes if available, but I was getting same error.
我一步一步跟随和/或尝试运行他们的源代码(如果可用),但我遇到了同样的错误。 I come to think there is some change in Swift 4 which makes these Swift 3 examples to produce error.
我开始认为 Swift 4 中有一些变化,这使得这些 Swift 3 示例产生错误。 As there are not many examples done in Swift 4, I decided I'd give a try by asking the question here.
由于在 Swift 4 中完成的示例并不多,我决定通过在此处提出问题来尝试一下。
The error I'm getting is:我得到的错误是:
this class is not key value coding-compliant for the key isLeaf.
此类不符合键 isLeaf 的键值编码。
I believe that error is coming from the key path set up for NSTreeController:我相信错误来自为 NSTreeController 设置的关键路径:
However I am not sure what needs to be done to fix the error.但是我不确定需要做什么来修复错误。
I have simple model class called Year.我有一个名为 Year 的简单模型类。
class Year: NSObject {
var name: String
init(name: String) {
self.name = name
}
func isLeaf() -> Bool {
return true
}
}
My view controller looks like this.我的视图控制器看起来像这样。
class ViewController: NSViewController, NSOutlineViewDataSource, NSOutlineViewDelegate {
@IBOutlet weak var outlineView: NSOutlineView!
@IBOutlet var treeController: NSTreeController!
override func viewDidLoad() {
super.viewDidLoad()
addData()
outlineView.delegate = self
outlineView.dataSource = self
}
func addData() {
let root = ["name": "Year", "isLeaf": false] as [String : Any]
let dict: NSMutableDictionary = NSMutableDictionary(dictionary: root)
dict.setObject([Year(name: "1999"), Year(name: "2000")], forKey: "children" as NSCopying)
treeController.addObject(dict)
}
func isHeader(item: Any) -> Bool {
if let item = item as? NSTreeNode {
return !(item.representedObject is Year)
} else {
return !(item is Year)
}
}
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
if isHeader(item: item) {
return outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "HeaderCell"), owner: self)!
} else {
return outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "DataCell"), owner: self)!
}
}
}
When I run the program, it causes no issue, but when I expand the node to show the two children of the root, it is giving the error I mentioned above.当我运行程序时,它不会引起任何问题,但是当我展开节点以显示根的两个子节点时,它给出了我上面提到的错误。
The class to which you are binding needs to be KVO compliant.您绑定到的类需要符合 KVO。
So, it needs to be a subclass of NSObject.所以,它需要是 NSObject 的子类。 And the objc runtime needs access.
并且 objc 运行时需要访问。
One way to do this:一种方法:
@objcMembers
class FileSystemItem: NSObject {
Or, you can annotate each field/function with @objc或者,您可以使用 @objc 注释每个字段/函数
Because is isLeaf
is used in KVO by NSOutlineView
, you have to add @objc
in front of isLeaf
function:因为
isLeaf
在 KVO 中被NSOutlineView
,所以你必须在isLeaf
函数前添加@objc
:
@objc func isLeaf() -> Bool {
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.