简体   繁体   English

NSTreeController 错误 - 此类不符合键的键值编码

[英]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 注释每个字段/函数

Full Example完整示例

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.

相关问题 类别对键@sum的键值编码不兼容 - class is not key value coding-compliant for the key @sum NSAlert() runmodal 此 class 不符合键 modalWindow 的键值编码 - NSAlert() runmodal this class is not key value coding-compliant for the key modalWindow 实体不符合密钥的密钥值编码要求 - Entity is not key value coding-compliant for the key 使用sortedArrayUsingDescriptors排序时出错:“类不是密钥值编码兼容的密钥” - Error while sorting with sortedArrayUsingDescriptors : “class is not key value coding-compliant for the key” Xcode - 如何修复“NSUnknownKeyException”,原因:……这个 class 与键 X 的键值编码不兼容”错误? - Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error? Cocoa KVC:“类不符合关键值编码” - Cocoa KVC: “class is not key value coding-compliant” “此类不符合键值编码标准”是什么意思? - What does “this class is not key value coding-compliant” mean? 实体xxx不符合键“(null)”的键值编码 - The entity xxx is not key value coding-compliant for the key “(null)” 如何跟踪KVC异常的来源:此类不​​适用于键工具栏的键值编码兼容? - How do I track down the source of a KVC exception: this class is not key value coding-compliant for the key toolbar? NSCache:尝试访问缓存项时始终获得“不符合键值编码” - NSCache: Always getting “not key value coding-compliant” when trying to access cached item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM