0

我正在使用 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:]:此类不符合键值编码为关键 plugName 。2015-01-21 19:47:07.348 NotifyHub [58369:598950](0 corefoundation 0x00007fff9220b64c __ exceptionprepreocess
+ 172 1 libobjc.a.dylib
0x00000000007fff950959666dection_ectiry +192020220220220220220220220220220220220FYENEFENDENDENEFERFIIND +
7.F.
0x00007fff8bd6c5ed -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226 4 Foundation 0x00007fff8bc89e68 -[NSObject(NSKeyValueCoding) valueForKey:] + 385 5 Foundation 0x00007fff8bca28e3 -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 324 6 Foundation 0x00007fff8bca28af -[NSObject(NSKeyValueCoding) valueForKeyPath :] + 272 7 AppKit 0x00007fff8975c8bf -[NSBinder valueForBinding:resolveMarkersToPlaceholders:] + 164 8 AppKit
0x00007fff89760700 -[NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] + 302 9 AppKit 0x00007fff8976053b -[NSValueBinder _observeValueForKeyPath:ofObject:context:] + 276 10 AppKit 0x00007fff897603ab -[NSTextValueBinder _observeValueForKeyPath:ofObject:context:] + 43 11 基础 0x00007fff8bc63c73 NSKeyValueNotifyObserver + 382 12 基础
0x00007fff8bc62e48 NSKeyValueDidChange + 463 13 基础
0x00007fff8bc679ad -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 118 14 AppKit
0x00007fff89eb5e83 -[NSCollectionView newItemForRepresentedObject:] + 87 15 AppKit 0x00007fff89ebce03 -[NSCollectionView _getItemsToDisplay] + 1168 16 AppKit 0x00007fff89eb5595 -[NSCollectionView setContent:] + 217 17 AppKit
0x00007fff89e8e965 -[NSCollectionViewBinder _updateContent] + 93 18 AppKit 0x00007fff89e8e9f0 -[NSCollectionViewBinder _observeValueForKeyPath:ofObject:上下文:] + 84 19 基金会 0x00007fff8bc63c73 NSKeyValueNotifyObserver + 382 20 基金会
0x00007fff8bca22a6 -[NSObject(NSKeyValueObservingPrivate) _notifyObserversForKeyPath:change:] + 1142 21 AppKit 0x00007fff897533ba -[NSController _notifyObserversForKeyPath:change:] + 206 22 AppKit 0x00007fff8984f126 -[NSArrayController didChangeValuesForArrangedKeys:objectKeys:indexKeys:] + 54 23 AppKit 0x00007fff89a1dbd0 -[NSArrayController _insertObject: atArrangedObjectIndex:objectHandler:] + 514 24 AppKit 0x00007fff89a1d82c -[NSArrayController addObject:] + 153 25 Notifyhub 0x000000010000166a _TFC9Notifyhub15NHAppController12awakeFromNibfS0_FT_T_ + 362 26 Notifyhub 0x00000001000016d2 _TToFC9Notifyhub15NHAppController12awakeFromNibfS0_FT_T_ + 34 27 CoreFoundation 0x00007fff9211983f -[NSSet makeObjectsPerformSelector:] + 223 28 AppKit
0x00007fff89ba2ffd -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1216 29 AppKit
0x00007fff89c22122 -[NSNib _instantiateNibWithExternalNameTable:options:] + 677 30 AppKit 0x00007fff89c22236 -[NSNib _instantiateWithOwner:options:topLevelObjects:] + 143 31 AppKit 0x00007fff89d5f36c -[NSStoryboard instantiateControllerWithIdentifier:] + 181 32 AppKit
0x00007fff896afef4 NSApplicationMain + 840 33 Notifyhub
0x00000001000011c2 top_level_code + 34 34 Notifyhub
0x00000001000011fa main + 42 35 libdyld.dyli

4

1 回答 1

5

我自己得到了答案。在此处发布以帮助 Swift 的任何可怜的新人。

在 Swift KVO 中不再默认支持,当你声明一个变量时必须设置“动态”如下:

dynamic var plugID : Int
dynamic var plugName : String
于 2015-01-21T12:31:15.250 回答