CoreData:无法为实体加载类 [英] CoreData: Unable to load class for entity
问题描述
我在使用Core Data中的关系时遇到问题。我创建了包括以下实体的数据模型:用户,对话,消息,参与者-每个实体都包含一个与它后面的实体一对多的关系。我使用编辑器->创建NSManagedObject子类为每个实体生成了类,并且为每个类正确创建了.Swift文件。该项目已生成,但是在尝试创建和保存新用户时出现以下错误:
I'm having an issue using relationships in Core Data. I created my data model including the following entities: User, Conversation, Message, Participant - each containing a one-to-many relationship with the entity following it. I generated the classes for each entity using Editor -> Create NSManagedObject Subclass, and it correctly created the .Swift files for each class. The project builds, but when attempting to create and save a new user I get the following error:
2014-12-01 12:31:28.450 Messenger[2627:151403] CoreData: warning: Unable to load class named 'Messenger.User' for entity 'User'. Class not found, using default NSManagedObject instead.
我确保我的实体类以项目/模块名称(Messenger.User)为前缀。
I made sure that my entity classes were prefixed with the project/module name (Messenger.User).
我还直接在User类上方添加了 @ObjC(User),并为该项目的 Other Linker Flags添加了 -ObjC,如其他职位的人这些都是我可以找到的所有修复程序,但是我仍然遇到相同的错误。这是我的User类的样子,供参考:
I also added "@ObjC(User)" directly above the User class, and added "-ObjC" to "Other Linker Flags" for the project, as suggested by people in various other posts. These are all the fixes that I could find, but I still get the same error. Here's what my User class looks like, for reference:
import Foundation
import CoreData
@objc(User)
class User: NSManagedObject {
@NSManaged var api : API
@NSManaged var username: String
@NSManaged var userID: String
@NSManaged var passcode: String
@NSManaged var conversations: NSSet
func findConversations(sourceView: MessengerViewController) {
api.findConversations(sourceView)
}
func addConversation(newConversation: Conversation) {
self.addConversationObject(newConversation)
}
}
extension User {
func addConversationObject(value: Conversation) {
var items = self.mutableSetValueForKey("conversations");
items.addObject(value)
}
func removeConversationObject(value: Conversation) {
var items = self.mutableSetValueForKey("conversations");
items.removeObject(value)
}
}
有人知道我做错了什么吗?我已经尝试了所有可能遇到的修复,但是到目前为止似乎没有任何效果。
Does anybody have an idea what else I did wrong? I've tried every fix I could come across, but nothing has seemed to work so far.
编辑:在尝试创建新的User对象时,会出现警告下面的第三行:
The warning occurs when trying to create a new User object, at the third line below:
let userContext : NSManagedObjectContext = self.appDel.managedObjectContext!
let userEntity : NSEntityDescription = NSEntityDescription.entityForName("User", inManagedObjectContext: userContext)!
var newUser = User(entity: userEntity, insertIntoManagedObjectContext: userContext)
推荐答案
参考我自己的 answer ,也许您还应该确保自己播放 >将任何结果提取到适当的类。例如,
Referring to my own answer, maybe you should also make sure you cast any fetch result to the appropriate class. E.g.
let result = context.executeFetchRequest(request, error:nil) as [User]
为响应代码更新,您可能应该尝试如下插入新实例。
In response to your code update, you should perhaps try to insert new instances as follows.
var user = NSEntityDescription.insertNewObjectForEntityForName( "User",
inManagedObjectContext: context) as User
这篇关于CoreData:无法为实体加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!