Swift CoreData NSManagedObject子类 [英] Swift CoreData NSManagedObject subclass

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

问题描述

我最近想试用CoreData及其功能,因此我找到了一个简单的教程,并在遵循它的同时,创建了一个具有实体和关系的.xcdatamodeld文件。



我现在要做的是将其转换为类(尝试使用Xcode版本8.1(8B62)和Xcode版本8.2 beta(8C23))



为此,我打开了模型文件,单击了 Editor -> 创建NSManagedObject子类
这样做并选择我的两个实体将生成4个类,两个NSManagedObject类,并为每个类扩展一个扩展。



在添加或生成了这四个文件之后,Xcode不会编译该项目并发出以下错误:



Command /Applications/Xcode.app/Contents/Developer/Toolchains/ XcodeDefault.xctoolchain / usr / bin / swiftc失败,退出代码为1



有时它还会显示:无效的重新声明[受管理对象名称]



任何见解都会很棒,我应该自己创建类而不使用模型吗?还是CoreData在Objc中工作?那么我将切换到该...

解决方案

Xcode 8添加了对自动Core Data子类生成的支持,您可以阅读关于文档



您可以在以下文章中了解有关Core Data代码生成的更多信息:



核心数据代码生成


I recently wanted to try out CoreData and its power, so I found a simple tutorial and while following it, I created a .xcdatamodeld file with Entities and relationships.

What I wanted to do now is to turn it into classes (Tried on Xcode Version 8.1 (8B62) and Xcode Version 8.2 beta (8C23))

For this I opened my model file, clicked on Editor --> Create NSManagedObject subclasses. Doing this and selecting my two entities would generate 4 classes, two NSManagedObject classes and for each of them one extension.

After adding or rather generating those four files, Xcode won't compile the project and puts out the following error:

Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1

and sometimes it also says: Invalid redeclaration of [Managed Object Name]

Any insight would be awesome, should I create the classes myself without using a model? or does CoreData work in Objc? then I'd switch to that...

解决方案

Xcode 8 adds support for automatic Core Data subclass generation, which you can read about in the document What's New in Core Data in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0. New projects have automatic code generation turned on so when you created NSManagedObject subclasses manually, you created duplicates, which is causing the compiler error.

There are two ways to fix this. First, you could remove the NSManagedObject subclasses you manually created. Second you can turn off automatic code generation for your data model from the Data Model inspector by choosing Manual/None from the Codegen menu. If you're following a tutorial, I recommend the second option because the tutorial was most likely written before Apple added automatic Core Data subclass generation.

You can learn more about Core Data's code generation in the following article:

Core Data Code Generation

这篇关于Swift CoreData NSManagedObject子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆