I just finished roasting some coffee from Peru. I’ve never had it before, but it got me thinking about my Roaster On the Go “app” as I thought about the context that I could use to show you the solution to the compiler error described in the title. I used this to demonstrate Working with Unwind Segues Programmatically in Swift, but now I’m going to use it to show you how to resolve these errors that can crop up when you’re implementing
NSManagedObject subclasses for your Core Data Entities.
Scenario – Adding a data model
Now suppose that I’m ready to implement a subclass of NSManagedObject for the
Doing so right now using Xcode 8’s default settings is going to give me all kinds of issues.
To be clear, I’m using Xcode 8.2 for this scenario.
The error that I’m getting when I attempt to build after creating a subclass of
NSManagedObject for the
Product Entity is one of the following (depending on how you choose to create the
Invalid redeclaration of ‘Product’
‘Product’ is ambiguous for type lookup in this context
or in the compiler output
filename “Product+CoreDataClass.swift” used twice
This is a conflict with Xcode 8’s auto codegen for
NSManagedObject subclasses. In some versions of Xcode 8 (version 8.2, for example), this auto codegen is turned on by default. This can present a problem if you intend to manually create
NSManagedObjectSubclasses for your Entities.
To solve this problem, or to avoid it before it occurs, you essentially need to turn off Xcode 8’s auto code generation feature for
NSManagedObject subclasses if you intend to implement your
NSManagedObject subclasses manually (my personal preference).
Take a look at the steps below to get your project building again:
1 – Turn off automatic code generation for NSManagedObject subclasses
Open your data model (the .xcdatamodeld file in your project).
Click on each Entity in your data model and in the Data Model Inspector of the Utilities Pane, change the Codegen property from “Class Definition” or “Category/Extension” to “Manual/None”:
2 – Choose module
Save your .xcdatamodeld file.
3 – Clear derived data
Behind the scenes, Xcode creates files and implements Types for the Entities in your data model. It puts them in your derived data folder and automatically traverses this folder to look for Type definitions for Core Data Entities. So in order to get your project building again, you need to get rid of those files that Xcode created automatically.
The “big hammer” for this is to simply clear all of your project’s derived data. To do this, click on the Products menu, hold down the Option key, and click “Clean build folder…”:
Alternatively, you could opt to go in and delete the only auto-generated Swift files, but you’ve got to do some digging.
Next, find the folder for your project at the root of your default derived data directory (where you were taken in Finder when you clicked the arrow in Project Settings). Then get ready to dig…
Click Build -> Intermediates -> ProjectName.build -> Debug-iphonesimulator -> ProjectName.build -> DerivedSources -> CoreDataGenerated -> ProjectName
4 – Rebuild
After you rebuild, any compiler errors you previously had, related to “ambiguous Type” or “redeclaration of Type” should be resolved!