我刚尝试用swift创建我的第一个OSX-Application。对于我的项目,我需要一个与CoreData一起使用的CollectionView。我按照tutorial的方式构建我的应用程序。
教程和我的项目之间的唯一区别是,我使用CoreData为我的对象加载数据。
我创建了一个条目然后Xcode抛出了这个错误:
2015-12-29 22:29:46.105 toRep_Management [11088:4844788] *由于未捕获的异常终止应用' NSUnknownKeyException',原因:' [valueForUndefinedKey:]:实体通知不是密钥值编码兼容的密钥" dateText"。' * 第一次抛出调用堆栈: ( 0 CoreFoundation 0x00007fff96614ae2 __exceptionPreprocess + 178 1 libobjc.A.dylib 0x00007fff8d9cff7e objc_exception_throw + 48 2 CoreFoundation 0x00007fff96614679 - [NSException raise] + 9 3 CoreData 0x00007fff9a54ad91 - [NSManagedObject valueForUndefinedKey:] + 289 4 Foundation 0x00007fff8d5229f0 - [NSObject(NSKeyValueCoding)valueForKeyPath:] + 272 5 AppKit 0x00007fff986393a0 - [NSBinder valueForBinding:resolveMarkersToPlaceholders:] + 164 6 AppKit 0x00007fff9863c936 - [NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] + 305 7 AppKit 0x00007fff9863c775 - [NSValueBinder _observeValueForKeyPath:ofObject:context:] + 296 8 AppKit 0x00007fff986dcec3 - [NSTextValueBinder _observeValueForKeyPath:ofObject:context:] + 43 9基金会0x00007fff8d528a53 NSKeyValueNotifyObserver + 379 10基金会0x00007fff8d534c23 NSKeyValueDidChange + 457 11基金会0x00007fff8d51dba4 - [NSObject(NSKeyValueObservingPrivate)_changeValueForKey:key:key:usingBlock:] + 1148 12基金会0x00007fff8d579c91 _NSSetObjectValueAndNotify + 274 13 AppKit 0x00007fff98893c6c - [NSCollectionView newItemForRepresentedObject:] + 228 14 AppKit 0x00007fff98893736 - [NSCollectionView _getItemsToDisplay] + 1209 15 AppKit 0x00007fff9889317a - [NSCollectionView setContent:] + 318 16 AppKit 0x00007fff98893005 - [NSCollectionViewBinder _updateContent] + 93 17 AppKit 0x00007fff98892f31 - [NSCollectionViewBinder _observeValueForKeyPath:ofObject:context:] + 84 18基金会0x00007fff8d528a53 NSKeyValueNotifyObserver + 379 19基金会0x00007fff8d532d7a - [NSObject(NSKeyValueObservingPrivate)_notifyObserversForKeyPath:change:] + 1127 20 AppKit 0x00007fff986307d8 - [NSController _notifyObserversForKeyPath:更改:] + 206 21 AppKit 0x00007fff9868d3e3 - [NSArrayController didChangeValuesForArrangedKeys:objectKeys:indexKeys:] + 54 22 AppKit 0x00007fff9884bcb5 - [NSArrayController _insertObject:atArrangedObjectIndex:objectHandler:] + 514 23 AppKit 0x00007fff9884b8e5 - [NSArrayController addObject:] + 153 24 toRep_Management 0x000000010000bb8d _TFC16toRep_Management16NoticeController12awakeFromNibfS0_FT_T_ + 1661 25 toRep_Management 0x000000010000bdc2 _TToFC16toRep_Management16NoticeController12awakeFromNibfS0_FT_T_ + 34 26 CoreFoundation 0x00007fff9651433f - [NSSet makeObjectsPerformSelector:] + 223 27 AppKit 0x00007fff98501ee4 - [NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1192 28 AppKit 0x00007fff986c580a - [NSNib _instantiateNibWithExternalNameTable:options:] + 677 29 AppKit 0x00007fff986c545a - [NSNib _instantiateWithOwner:options:topLevelObjects:] + 143 30 AppKit 0x00007fff98c4167a - [NSStoryboard instantiateControllerWithIdentifier:] + 181 31 AppKit 0x00007fff984da34e NSApplicationMain + 710 32 toRep_Management 0x000000010000e597 main + 87 33 libdyld.dylib 0x00007fff94ebd5ad start + 1 34 ??? 0x0000000000000003 0x0 + 3 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止
这是我的NoticeController的代码:
@IBOutlet weak var noticeArrayController: NSArrayController!
//CoreData
let context = (NSApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let fetchRequestForNotice = NSFetchRequest(entityName: "Notice")
var notices: NSMutableArray = NSMutableArray()
override func awakeFromNib() {
var dataNotices = [Notice]()
do {
dataNotices = try context.executeFetchRequest(fetchRequestForNotice) as! [Notice]
} catch {
print(error)
}
for var i = 0; i < dataNotices.count; i++ {
notices.addObject(dataNotices[i])
}
for var i = 0; i < notices.count; i++ {
let newNotice = NoticeObject()
newNotice.image = NSImage()
newNotice.title = notices[i].title!
newNotice.dateText = "iein Datum"
self.noticeArrayController.addObject(newNotice) // THREAD 1: SIGNAL SIGBART
}
}
这里是我的NoticeObject:
class NoticeObject: NSObject {
var image: NSImage = NSImage()
var title: String = ""
var dateText: String = "" }
你能帮助我吗?