Xcode 8 如何在 Objective-C 中使用核心数据代码生成类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41132790/
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
Xcode 8 How to use Core Data Code Gen Classes in Objective-C?
提问by SolidSnake4444
I was watching this tutorialto use Core Data in Xcode 8 and the video was using Swift. In the video, He entered an entity named Task
in the Core Data model and then in the code he was able to call Task
like:
我正在观看本教程以在 Xcode 8 中使用 Core Data,而视频使用的是 Swift。在视频中,他输入了一个Task
在 Core Data 模型中命名的实体,然后在代码中他可以Task
像这样调用:
let task = Task(context: context)
I have an entity called Tag
. How can I access Tag in code using Objective-C? Codegen is set in the Core Data model but I don't see any additional files in my project. If I try:
我有一个名为Tag
. 如何使用 Objective-C 在代码中访问 Tag?Codegen 设置在 Core Data 模型中,但我在我的项目中没有看到任何其他文件。如果我尝试:
Tag test = [[Tag alloc] init];
I get an error that Tag
does not exist.
我收到一个Tag
不存在的错误。
采纳答案by Satachito
Select the entity 'Tag' in model editor.
Generate source code for Task by selecting menu tree 'Editor' -> 'Create NSManagedObject Subclass...' then follow the instruction.
在模型编辑器中选择实体“标签”。
通过选择菜单树“编辑器”->“创建 NSManagedObject 子类...”为任务生成源代码,然后按照说明进行操作。
'Tag+CoreDataClass.h'
'Tag+CoreDataClass.m'
'Tag+CoreDataProperties.h'
'Tag+CoreDataProperties.m'
'标签+CoreDataClass.h'
'标签+CoreDataClass.m'
'标签+CoreDataProperties.h'
'标签+CoreDataProperties.m'
files will be created and automatically will be attached to your project.
文件将被创建并自动附加到您的项目中。
Import header file.
#import "Tag+CoreDataProperties.h"
Then create 'Tag' class.
NSManagedObjectContext *wContext = ((AppDelegate *)UIApplication.sharedApplication.delegate).persistentContainer.viewContext; Tag *wTag = [[Tag alloc] initWithContext:wContext]; wTag.name = @"TEST";
导入头文件。
#import "Tag+CoreDataProperties.h"
然后创建“标签”类。
NSManagedObjectContext *wContext = ((AppDelegate *)UIApplication.sharedApplication.delegate).persistentContainer.viewContext; Tag *wTag = [[Tag alloc] initWithContext:wContext]; wTag.name = @"TEST";
回答by Ryan H.
If Codegen is set to "Class Definition" then you can just import your entity's NSManagedObject
subclass header file.
如果 Codegen 设置为“类定义”,那么您只需导入实体的NSManagedObject
子类头文件即可。
Import:
进口:
#import "Tag+CoreDataClass.h"
Then the creation of your Tag
object will be recognized.
然后您的Tag
对象的创建将被识别。
Tag *tag = [NSEntityDescription insertNewObjectForEntityForName:@"Tag" inManagedObjectContext:[self managedObjectContext];
tag.name = @"Tag Name";
Note:If you want to see the files that were generated on your behalf, you can check the DerivedData folder for your project. You should not edit these files or import them into your project.
注意:如果您想查看代表您生成的文件,您可以检查项目的 DerivedData 文件夹。您不应编辑这些文件或将它们导入到您的项目中。
Something like:
就像是:
/Users/{Username}/Library/Developer/Xcode/DerivedData/{Your Project Name}/Build/Intermediates/{Your Project Name}.build/Debug-iphonesimulator/{Your Project Name}.build/DerivedSources/CoreDataGenerated/{Your Project Name}/
/Users/{Username}/Library/Developer/Xcode/DerivedData/{Your Project Name}/Build/Intermediates/{Your Project Name}.build/Debug-iphonesimulator/{Your Project Name}.build/DerivedSources/CoreDataGenerated/{Your Project Name}/
There are other Codegen options that offer different options depending on your use case:
还有其他 Codegen 选项可根据您的用例提供不同的选项:
- None/Manual: Allows you to manage the
NSManagedObject
subclasses yourself. With this option, you will see the files in your project and you can modify them. - Category/Extension: Allows you to have custom properties (attributes) that you do not want Core Data to manage.
- 无/手动:允许您自己管理
NSManagedObject
子类。使用此选项,您将看到项目中的文件并可以修改它们。 - 类别/扩展:允许您拥有不想让 Core Data 管理的自定义属性(属性)。
I posted a more detailed answer regarding Codegen options here: https://stackoverflow.com/a/40647786/4748172
我在这里发布了关于 Codegen 选项的更详细的答案:https://stackoverflow.com/a/40647786/4748172