Unable to find specific subclass of NSManagedObject(找不到 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 的特定子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:找不到 NSManagedObject 的特定子类
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01