Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data classes not generated for test target

I use Core Data's automatically generated classes. My project has 3 targets in addition to the test target. For each target, the Core Data classes are properly generated which I verified by inspecting the Derived Data folder. However, classes are not generated for the Test Target despite it being ticked in the Core Data model file. This causes an "undeclared identifier" and "Use of undeclared type" errors when I try to reference one of the Core Data classes in the test target. How can I fix this please?

like image 760
RunLoop Avatar asked Mar 31 '17 12:03

RunLoop


4 Answers

You do not need extra classes generated for each test target - your import process should import everything, and no files should need to be added to other targets.

Declaring @testable import MyProject should take care of everything.

In Objective C

@import MyProject;
like image 110
sschale Avatar answered Nov 10 '22 04:11

sschale


In Xcode 9.1 try adding your .xcdatamodel to a test target too. All auto-generated class will be imported too.

like image 30
SoftDesigner Avatar answered Nov 10 '22 04:11

SoftDesigner


This was due to a bug currently in Xcode (8.3.1) where auto-generated NSManagedObject classes (codegen set to "Class Definition") cannot be found on the global path despite the project compiling successfully. The only way around it is to which to manual generation of the NSManagedObject classes by setting codegen for each entity to "Manual/None".

like image 2
RunLoop Avatar answered Nov 10 '22 03:11

RunLoop


Select the test target, navigate to Build Settings and search for the setting "Header Search Paths"

Then add the following entry:

$CONFIGURATION_TEMP_DIR/{Project Target Name}.build/DerivedSources/CoreDataGenerated/{Project Name}

Replace the curly brackets with your main target name (not the test target), and your project name, respectively.

Xcode should now be able to find the generated source files when building the test target.

like image 2
Daniel Stafford Avatar answered Nov 10 '22 03:11

Daniel Stafford