我使用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 Notifyhub [58369:598950](0 CoreFoundation
0x00007fff9220b64c __exceptionPreprocess + 172 1 libobjc.A.dylib
0x00007fff950966de objc_exception_throw + 43 2 CoreFoundation
0x00007fff9220b1e9 - [NSException raise] + 9 3基础
0x00007fff8bd6c5ed - [NSObject(NSKeyValueCoding)valueForUndefinedKey:] + 226 4 Foundation 0x00007fff8bc89e68 - [NSObject(NSKeyValueCoding)valueForKey:] + 385 5 Foundation 0x00007fff8bca28e3 - [NSObject(NSKeyValueCoding)valueForKeyPath:] + 324 6基金会0x00007fff8bca28af - [NSObject(NSKeyValueCoding)valueForKeyPath:] + 272 7 AppKit 0x00007fff8975c8bf - [NSBinder valueForBinding:resolveMarkersToPlaceholders:] + 164 8 AppKit
0x00007fff89760700 - [NSValueBinder _adjustObject:模式:observedController:observedKeyPath:上下文:editableState:adjustState:] + 302 9 AppKit 0x00007fff8976053b - [NSValueBinder _observeValueForKeyPath:ofObject:context:] + 276 10 AppKit 0x00007fff897603ab - [NSTextValueBinder _observeValueForKeyPath:ofObject:context:] + 43 11 Foundation 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:context:] + 84 19 Foundation 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
答案 0 :(得分:5)
我自己得到了答案。发布在这里为Swift帮助任何可怜的新人。
在Swift中,KVO不再是默认支持,当您声明变量时必须设置“动态”,如下所示:
dynamic var plugID : Int
dynamic var plugName : String