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

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

问题描述

我正在使用 Core Data 开发应用程序.当我使用以下方法创建实例时:

let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: appDelegate.managedObjectContext)让用户=用户(实体:实体,insertIntoManagedObjectContext:appDelegate.managedObjectContext)

我在日志中收到警告:

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

我该如何解决?

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

我已经指定了实体的类,如下图所示:

解决方案

Xcode 7 更新(最终版):不再需要将模块名称附加到类(如 Xcode 6 和 Xcode 7 的早期 beta 版本中).苹果文档

当您为实体创建 Swift 托管对象子类时,模块"字段设置为当前产品模块",并使用此设置创建实例既适用于主应用程序,也适用于单元测试.托管对象子类必须@objc(classname) 标记(这在 https://stackoverflow.com/a/31288029/1187415).

或者,您可以清空模块"字段(它将显示无")并标记带有 @objc(classname) 的托管对象子类(观察到在 https://stackoverflow.com/a/31287260/1187415).

<小时>

备注:此答案最初是为 Xcode 6 编写的.各种 Xcode 7 beta 版本有一些变化尊重这个问题.因为这是许多人公认的答案赞成票和链接,我试图总结情况对于当前的 Xcode 7 最终版本.

我做了自己的研究"并阅读了这个问题和类似问题的所有答案CoreData:警告:无法加载类命名.所以归属于他们所有人,即使我没有具体列出来!

<小时>

Xcode 6 的先前答案:

实现核心数据托管对象子类,你必须在模型实体检查器的 Class 字段中的实体类名称前加上您的模块名称,例如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 https://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 https://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天全站免登陆