1

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...

1 Answer 1

4

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.

enter image description here

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

Core Data Code Generation

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, so Xcode automatically creates hidden Swift classes based on what you define in the Model? So I can still use them in code, but they are not shown to me?
My best guess to your questions is Yes, but I haven't used the automatic subclass generation. As I said at the end of my answer, if you're following a tutorial, you should turn off the automatic subclass generation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.