ios 无法找到 NSManagedObject 的特定子类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25076276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to find specific subclass of NSManagedObject
提问by MsrButterfly
I'm working on developing an app with Core Data. When I created an instance using:
我正在使用 Core Data 开发应用程序。当我使用以下方法创建实例时:
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?
还有一个问题,如何在 NSManagedObject 子类中定义实例方法?
Edit:
编辑:
I have specified class of the entity as in the following screenshot:
我已指定实体的类,如下面的屏幕截图所示:
回答by Martin R
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 Subclasseshas been updated accordingly.
Xcode 7 更新(最终版):不再需要将模块名称添加到类中(如在 Xcode 6 和 Xcode 7 的早期测试版中)。Apple 文档 实现核心数据托管对象子类已相应更新。
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 notbe marked with @objc(classname)
(this was observed in https://stackoverflow.com/a/31288029/1187415).
当您为实体创建 Swift 托管对象子类时,“模块”字段设置为“当前产品模块”,并且使用此设置创建实例在主应用程序和单元测试中都有效。托管对象子类不得标记@objc(classname)
(这是在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).
或者,您可以清空“模块”字段(它将显示“无”)并标记托管对象子类@objc(classname)
(这在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.
备注:此答案最初是为 Xcode 6 编写的。在各种 Xcode 7 beta 版本中针对此问题进行了一些更改。由于它是一个被接受的答案,有很多赞成和链接,我试图总结当前 Xcode 7 最终版本的情况。
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!
我做了我自己的“研究”并阅读了这个问题和类似问题CoreData: warning: Unable to load class named 的所有答案 。因此,即使我没有具体列出它们,也归于所有人!
Previous answer for Xcode 6:
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".
如实现核心数据托管对象子类中所述,您必须在模型实体检查器的类字段中使用模块名称作为实体类名称的前缀,例如“MyFirstSwiftApp.User”。
回答by Christer
Just as a side-note. i had the same issue. And all i had to do was add @objc(ClassName)
in my class file.
只是作为一个旁注。我遇到过同样的问题。我所要做的就是添加@objc(ClassName)
到我的类文件中。
Example:
例子:
@objc(Person)
class Person { }
And that solved my issue.
这解决了我的问题。
回答by Mannix
The accepted answer to this question helped me resolve the same issue but I had a caveat that I thought would be helpful to others. If your project (module) name has a space in it you must replace the space with an underscore. For example:
这个问题的公认答案帮助我解决了同样的问题,但我有一个警告,我认为这会对其他人有所帮助。如果您的项目(模块)名称中有空格,则必须用下划线替换该空格。例如:
Entity: MyEntity Class: My_App_Name.MyClass
实体:MyEntity 类:My_App_Name.MyClass
回答by Bart?omiej Semańczyk
Remember to remove your module:
请记住删除您的模块:
回答by Ludovic Landry
Depending if you are running as App vs Tests the issue can be that the app is looking for <appName>.<entityName>
and when it's running as test it's looking as <appName>Tests.<entityName>
. The solution I use at this time (Xcode 6.1) is to NOT fill the Class
field in the CoreData UI, and to do it in code instead.
取决于您是作为 App 还是 Tests 运行,问题可能是应用程序正在寻找<appName>.<entityName>
,当它作为测试运行时,它看起来像<appName>Tests.<entityName>
. 我此时使用的解决方案(Xcode 6.1)是不填写Class
CoreData UI 中的字段,而是在代码中填写。
This code will detect if you are running as App vs Tests and use the right module name and update the managedObjectClassName
.
此代码将检测您是否作为 App vs Tests 运行并使用正确的模块名称并更新managedObjectClassName
.
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional...
let modelURL = NSBundle.mainBundle().URLForResource("Streak", withExtension: "momd")!
let managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)!
// Check if we are running as test or not
let environment = NSProcessInfo.processInfo().environment as [String : AnyObject]
let isTest = (environment["XCInjectBundle"] as? String)?.pathExtension == "xctest"
// Create the module name
let moduleName = (isTest) ? "StreakTests" : "Streak"
// Create a new managed object model with updated entity class names
var newEntities = [] as [NSEntityDescription]
for (_, entity) in enumerate(managedObjectModel.entities) {
let newEntity = entity.copy() as NSEntityDescription
newEntity.managedObjectClassName = "\(moduleName).\(entity.name)"
newEntities.append(newEntity)
}
let newManagedObjectModel = NSManagedObjectModel()
newManagedObjectModel.entities = newEntities
return newManagedObjectModel
}()
回答by Rien
If you are using a hyphen in your project name like "My-App" then use an underscore instead of the hyphen like "My_App.MyManagedObject". In general, look at the name of the xcdatamodeld file and use the same prefix as in that name. I.e. "My_App_1.xcdatamodeld" requires the prefix "My_App_1"
如果您在项目名称中使用连字符,例如“My-App”,则使用下划线而不是“My_App.MyManagedObject”等连字符。通常,查看 xcdatamodeld 文件的名称并使用与该名称相同的前缀。即“My_App_1.xcdatamodeld”需要前缀“My_App_1”
回答by enreas
This may help those experiencing the same problem. I was, with Swift 2 and Xcode 7 beta 2.
这可能会帮助那些遇到同样问题的人。我是,使用 Swift 2 和 Xcode 7 beta 2。
The solutionin my case was to comment out @objc(EntityName)
in EntityName.swift
.
在我的情况下,解决方案是@objc(EntityName)
在EntityName.swift
.
回答by Daniel Ford
I had the same warning, though my app appeared to run fine.
The problem was that when running Editor > Create NSManagedObject Subclass on the last screen I used the default Group location, with no Targets displayed or checked, which saved the subclass in the top MyApp directory where MyApp.xcodeproj was located.
The warning went away when I instead changed the Group to be in the MyApp subfolder and checked the MyApp target.
我有同样的警告,但我的应用程序似乎运行良好。问题是,在最后一个屏幕上运行 Editor > Create NSManagedObject Subclass 时,我使用了默认的 Group 位置,没有显示或检查 Targets,这将子类保存在 MyApp.xcodeproj 所在的 MyApp 目录中。
当我将组更改为 MyApp 子文件夹中并检查 MyApp 目标时,警告消失了。
回答by Mike
By the way be carful what you add as a prefix: My App is called "ABC-def" and Xcode has converted the "-" into a "_".
顺便说一句,小心你添加的前缀:我的应用程序被称为“ABC-def”,Xcode 已将“-”转换为“_”。
To be safe look into the finder, find your project files and see what it says for your data model (for example "ABC_def.xcdatamodeld") and use what is written there EXACTLY!!!
为了安全查看查找器,请找到您的项目文件并查看它对您的数据模型的说明(例如“ABC_def.xcdatamodeld”)并使用那里写的内容!
回答by trevorgrayson
The above answers were helpful. This quick sanity check may save you some time. Go into Project > Build Phases > Compile Sources and remove your xcdatamodeld and your model files with the "-" button, and then add them right back with the "+" button. Rebuild -- that may take care of it.
上面的回答很有帮助。这种快速的健全性检查可能会为您节省一些时间。进入 Project > Build Phases > Compile Sources 并使用“-”按钮删除您的 xcdatamodeld 和模型文件,然后使用“+”按钮将它们添加回来。重建 - 这可能会解决它。