insertNewObjectForEntityForName无法找到实体(Swift) [英] insertNewObjectForEntityForName could not locate entity (Swift)

查看:148
本文介绍了insertNewObjectForEntityForName无法找到实体(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将可工作的CoreData应用程序移植到Swift.此代码失败:

I am trying to port a working CoreData application to Swift. This code fails:

    let moc = self.context
    NSLog("Entities: \(moc.persistentStoreCoordinator.managedObjectModel.entitiesByName.allKeys)")
    let obj = NSEntityDescription.insertNewObjectForEntityForName("School",
        inManagedObjectContext:moc)

NSLog的输出是这样的:

2014-06-11 11:23:51.634 WYBData[18480:303] Entities [Address, Contact, Hospital, Person, Place, School]

因此,即使日志表明存在名为School的实体(我认为这表示NSManagedObjectContext的设置正确),我仍然会收到此错误:

So even though the log indicates there is an entity called School, which I assume means the NSManagedObjectContext is correctly set up, I still get this error:

2014-06-11 11:23:55.338 WYBData[18480:303] +entityForName: could not locate an entity named 'School' in this model.
2014-06-11 11:23:55.340 WYBData[18480:303] (
0   CoreFoundation                      0x00007fff8c05725c __exceptionPreprocess + 172
1   libobjc.A.dylib                     0x00007fff8f274e75 objc_exception_throw + 43
2   CoreData                            0x00007fff89b20a19 +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:] + 201
3   WYBData                             0x00000001000171f3 _TFC7WYBData12WYBXmlParser7executefS0_FRGSqCSo7NSError_Sb + 2403
4   WYBData                             0x000000010000ea4a _TFC7WYBData11WYBMainView2gofS0_FPSs9AnyObject_T_ + 1642
5   WYBData                             0x000000010000ead2 _TToFC7WYBData11WYBMainView2gofS0_FPSs9AnyObject_T_ + 66
6   AppKit                              0x00007fff91d97260 -[NSApplication sendAction:to:from:] + 327
7   AppKit                              0x00007fff91d970de -[NSControl sendAction:to:] + 86
8   AppKit                              0x00007fff91de3c4d -[NSCell _sendActionFrom:] + 128
9   AppKit                              0x00007fff91dfd655 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2316
10  AppKit                              0x00007fff91dfca27 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 487
11  AppKit                              0x00007fff91dfc13d -[NSControl mouseDown:] + 706
12  AppKit                              0x00007fff91d7da58 -[NSWindow sendEvent:] + 11296
13  AppKit                              0x00007fff91d1c5d4 -[NSApplication sendEvent:] + 2021
14  AppKit                              0x00007fff91b6ca19 -[NSApplication run] + 646
15  AppKit                              0x00007fff91b577a3 NSApplicationMain + 940
16  WYBData                             0x000000010000a9ed top_level_code + 109
17  WYBData                             0x000000010000aa2a main + 42
18  libdyld.dylib                       0x00007fff8bf085fd start + 1
19  ???                                 0x0000000000000003 0x0 + 3
)

我可以正确创建一个Address实体,但不能创建其他任何实体.甚至这很奇怪,因为AddressContact的子实体,但是尝试创建Contact会导致上面的相同错误.

I can correctly create an Address entity, but none of the others. Even this is strange because Address is a sub-entity of Contact yet attempting to create Contact results in the same error above.

我想念什么?

我暂时从核心数据模型中删除了Address,现在可以创建Contact了,但是没有别的了.因此,看来我只能创建模型中定义的第一个实体(按字母顺序).这听起来像是错误,还是我的NSManagedObjectModel可能以某种方式损坏了?

I temporarily removed Address from my Core Data model, and can now create Contact but nothing else. So it seems that I can only create whatever is the first entity (alphabetically) defined in the model. Does this sound like a bug, or could my NSManagedObjectModel be corrupted somehow?

推荐答案

对于遇到相同问题的其他任何人:问题是我的.xcdatamodeld文件.它一定已经损坏了,因为我创建了一个新文件并完全重新创建了我的数据模型,现在它可以工作了.我看不到两个文件之间的任何区别(至少在Xcode Core Data模型编辑器中).

For anyone else who encounters the same problem: the issue was my .xcdatamodeld file. It must have been corrupted somehow because I created a new file and exactly re-created my data model and now it works. I cannot see any difference between the two files (at least in the Xcode Core Data model editor).

我的数据模型在iOS和OS-X项目之间共享(OS-X应用程序为iOS应用程序准备数据).我想知道为iOS创建的.xcdatamodeld文件与OS-X是否有任何区别.

My data model is shared between an iOS and OS-X project (the OS-X application prepares the data for the iOS app). I wondered if there was any difference in the .xcdatamodeld files created for iOS compared with OS-X.

这篇关于insertNewObjectForEntityForName无法找到实体(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆