Core Data: Solving Ambiguous Type / Redeclaration of Type

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

The “app” doesn’t currently use Core Data, but for this article, I’ve created a rudimentary data model that includes a single Entity called Product:
RoasterOnTheGo_xcdatamodel

Now suppose that I’m ready to implement a subclass of NSManagedObject for the Product Entity.

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 NSManagedObject subclass):

Invalid redeclaration of ‘Product’

or

‘Product’ is ambiguous for type lookup in this context

or in the compiler output

filename “Product+CoreDataClass.swift” used twice

What’s happening??

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.

Solution

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”:

Turn code generation off

2 – Choose module

You also need to tell Xcode where it can look to find a definition for the NSManagedObject subclass that you will implement for this Entity by choosing “Current Product Module” for the Module value:
Choose "Current Product Module" as module value

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…”:

Product -> Press Option Key -> 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.

Click on File -> Project Settings:
File -> Project Settings

Next, click on the arrow next to the path leading to your default derived data folder:
Click derived data folder arrow

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

Delete the .swift files in this folder.
Path to auto-generated Entities

4 – Rebuild

After you rebuild, any compiler errors you previously had, related to “ambiguous Type” or “redeclaration of Type” should be resolved!