无法找到NSManagedObject的特定子类 [英] Unable to find specific subclass of NSManagedObject

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

问题描述

我正在使用Core Data开发应用程式。当我创建一个实例使用:

  let entity = NSEntityDescription.entityForName(User,inManagedObjectContext:appDelegate.managedObjectContext)
let user = User(entity:entity,insertIntoManagedObjectContext:appDelegate.managedObjectContext)

在日志中:

  CoreData:警告:无法加载实体'User'的名为'User'的类。找不到类,而是使用默认的NSManagedObject。 

如何解决?



另一个问题,如何在NSManagedObject子类中定义一个实例方法?



编辑:



实体类别,如以下屏幕截图所示:



解决方案

更新Xcode 7(最终版):
类的模块名称(如Xcode 6和Xcode 7的早期beta版本)不再需要。
Apple文档



当为实体创建Swift管理对象子类时,
Module字段设置为当前产品模块,并且使用此设置
创建实例工作在主应用程序和单元测试。
托管对象子类必须 标记为 @objc(classname)(这在http://stackoverflow.com/a/31288029/1187415 )。



或者,您可以清空模块字段(它将显示无),并使用 @objc(classname)管理对象子类>(这在 http://stackoverflow.com/a/31287260/1187415 中被观察为
)。






注意:这个答案最初是为Xcode 6编写的。
使用
的各种Xcode 7测试版本的变化与这个问题相关。因为它是一个接受的答案许多
upvotes和链接到它,我试图总结情况
当前Xcode 7最终版本。



我做了我自己的研究,并阅读所有的答案这两个问题和类似的问题
CoreData:warning:无法加载名为的类。因此,归因归属于所有人,即使我不是
明确列出他们!






上一个答案 Xcode 6



实现Core Data Managed Object子类,您必须
在模型实体检查器的类字段中使用模块的名称(例如MyFirstSwiftApp.User)为实体类名添加前缀。


I'm working on developing an app with Core Data. When I created an instance using:

let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: appDelegate.managedObjectContext)
let user = User(entity: entity, insertIntoManagedObjectContext: appDelegate.managedObjectContext)

I got a warning in log:

CoreData: warning: Unable to load class named 'User' for entity 'User'.  Class not found, using default NSManagedObject instead.

How could I fix it?

And another question, how can I define an instance method in NSManagedObject subclass?

Edit:

I have specified class of the entity as in the following screenshot:

解决方案

Update for Xcode 7 (final): Prepending the module name to the class (as in Xcode 6 and early beta releases of Xcode 7) is no longer necessary. The Apple documentation Implementing Core Data Managed Object Subclasses has been updated accordingly.

The Data Model inspector has now two fields "Class" and "Module" for an entity:

When you create a Swift managed object subclass for the entity, the "Module" field is set to "Current Product Module", and with this setting creating instances works both in the main application and in unit tests. The managed object subclass must not be marked with @objc(classname) (this was observed in http://stackoverflow.com/a/31288029/1187415).

Alternatively, you can empty the "Module" field (it will show "None") and mark the managed object subclasses with @objc(classname) (this was observed in http://stackoverflow.com/a/31287260/1187415).


Remark: This answer was originally written for Xcode 6. There were some changes in the various Xcode 7 beta releases with respect to this problem. Since it is an accepted answer with many upvotes and links to it, I have tried to summarize the situation for the current Xcode 7 final version.

I did both my own "research" and read all the answers to both this question and the similar question CoreData: warning: Unable to load class named. So attribution goes to all of them, even if I don't list them specifically!


Previous answer for Xcode 6:

As documented in Implementing Core Data Managed Object Subclasses, you have to prefix the entities class name in the Class field in the model entity inspector with the name of your module, for example "MyFirstSwiftApp.User".

这篇关于无法找到NSManagedObject的特定子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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