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

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

问题描述

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

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

我在日志中收到警告:

CoreData:警告:无法为实体User"加载名为User"的类.未找到类,使用默认 NSManagedObject 代替.

我该如何解决?

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

我已指定实体的类,如下面的屏幕截图所示:

解决方案

Xcode 7 更新(最终版):不再需要在类之前添加模块名称(如在 Xcode 6 和 Xcode 7 的早期测试版中).Apple 文档

当你为实体创建一个 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天全站免登陆