我正在使用Swift为NSCollectionView工作。当我使用NSArrayController对象作为NSCollectionView的内容时。我发现以下错误:
引发了意外异常
[valueForUndefinedKey:]:此类不符合密钥plugName的密钥值编码。
我不知道为什么。我创建了一个名为NHPlug的模型和一个NSArrayController,如下所示,这是我的代码。:

/* Class NHAppController */
import Cocoa

class NHAppController: NSObject {
    @IBOutlet weak var arrayController: NSArrayController?

    var plugs: NSMutableArray?

    override func awakeFromNib() {
        var t = NHPlug(plugID: 1, plugName: "test")
        plugs = NSMutableArray()
        arrayController?.addObject(t)
    }
}

/* Class NSPlug */
import Cocoa

class NHPlug: NSObject {

    var plugID : Int
    var plugName : String

    init(plugID: Int, plugName: String) {
        self.plugID = plugID
        self.plugName = plugName;
    }

}

如果有人能帮忙,非常感谢!
如果崩溃堆栈:
-01-21 19:47:07.347 Notifyhub[58369:598950]引发了未捕获的异常2015-01-21 19:47:07.347 Notifyhub[58369:598950]
[valueForUndefinedKey:]:此类
不符合密钥插件名的密钥值编码。2015年1月21日
19:47:07.348通知中心[58369:598950](0 CoreFoundation
0x00007fff9220b64c异常预处理+172 1 libobjc.A.dylib
0x00007fff950966de objc_exception_throw+43 2核心基础
0x00007fff9220b1e9-[N异常升高]+9 3基础
0x00007fff8bd6c5ed-[NSObject(NSKeyValueCoding)valueForUndefinedKey:]
+226 4基础0x00007fff8bc89e68-[NSObject(NSKeyValueCoding)valueForKey:]+385 5基础0x00007fff8bca28e3-[NSObject(NSKeyValueCoding)valueForKeyPath:]+
324 6基础0x00007fff8bca28af
-[NSObject(NSKeyValueCoding)valueForKeyPath:]+272 7 AppKit 0x00007fff8975c8bf-[NSBinder
绑定值:resolveMarkersToPlaceholders:+164 8 AppKit
0x00007fff89760700-[NSValueBinder
_adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:]
+302 9 AppKit 0x00007fff8976053b-[NSValueBinder\u observeValueForKeyPath:ofObject:context:]+276 10 AppKit 0x00007fff897603b
-[NSTextValueBinder\u observeValueForKeyPath:ofObject:context:]+43 11基础0x00007fff8bc63c73
NSKeyValueNotifyObserver+382 12基础
0x00007fff8bc62e48 NSKeyValueDidChange+463 13基础
0x00007fff8bc679ad-[NSObject(nskeyvalueobserver通知)
didChangeValueForKey:+118 14应用工具包
0x00007fff89eb5e83-[NSCollectionView代表对象的新项:]+
87 15配件包0x00007fff89ebce03
-[NSCollectionView\u getItemsToDisplay]+1168 16 AppKit 0x00007fff89eb5595-[NSCollectionView设置内容:]+217 17 AppKit
0x00007fff89e8e965-[NSCollectionViewBinder更新内容]+93 18
应用工具包0x00007fff89e8e9f0
-[NSCollectionViewBinder\u observeValueForKeyPath:ofObject:context:+84 19基础0x00007fff8bc63c73
NSKeyValueNotifyObserver+382 20基础
0x00007fff8bca22a6-[NSObject(NSKeyValueObservingPrivate)
_notifyObserversForKeyPath:更改:]+1142 21 AppKit 0x00007fff897533ba-[NSController\u notifyObserversForKeyPath:更改:]
+206 22 AppKit 0x00007fff8984f126-[NSArrayController更改rarrangedkeys的值:objectKeys:indexKeys:]+54 23 AppKit
0x00007fff89a1dbd0-[nsarray控制器
_插入对象:atarangedobjectindex:objectHandler:]+514 24 AppKit 0x00007fff89a1d82c-[NSArrayController addObject:]+153 25
通知中心0x000000010000166a
_TFC9Notifyhub15NHAppController12awakeFromNibfS0_uft_u+362 26 Notifyhub 0x00000001000016d2
_TTOFC9通知HUB15NHAPPController12awakefromnibfs0_FT_T_+34 27 CoreFoundation 0x00007fff9211983f-[NSSet
makeObjectsPerformSelector:+223 28应用工具包
0x00007fff89ba2ffd-[NSIBObjectData
nibInstanceWithowner:选项:topLevelObjects:+1216 29 AppKit
0x00007fff89c22122-[NSNib
_实例化具有外部名称表:选项:]+677 30 AppKit 0x00007fff89c22236-[NSNib
_实例化所有者:选项:topLevelObjects:+143 31 AppKit 0x00007fff89d5f36c-[NSStoryboard
实例化控制器标识符:]+181 32 AppKit
0x00007fff896afef4 NSApplicationMain+840 33通知中心
0x00000001000011c2顶层代码+34 34 Notifyhub
0x00000001000011fa主+42 35 libdyld.dyli

最佳答案

我自己找到答案了。寄到这里来帮助任何一个可怜的新人。
在Swift中,KVO不再是默认支持,当您声明一个变量时,必须将其设置为“dynamic”,如下所示:

dynamic var plugID : Int
dynamic var plugName : String

关于macos - 使用NSArrayController时,出现“valueForUndefinedKey:此类不适用于键错误的键值编码兼容”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28066947/

10-13 23:58