In Chapter 22, in the section titled “NSManagedObject and Subclasses,” we read the instructions:
‘Open Photorama.xcdatamodeld. Select the Photo entity and open the Data Model inspector. Locate the Codegen option and select Manual/ None.’
This is fine, however a bug in Xcode 8.2.1 (and some earlier releases) may affect you when you complete the section and build/run the project. You may get the compile-time errors:
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
Which is caused by an error similar to this output:
:0: error: filename “Entity+CoreDataClass.swift” used twice: ‘/Users/greg2/Desktop/Current/Swift/TestCD/Photo+CoreDataClass.swift’ and ‘/Users/greg2/Library/Developer/Xcode/DerivedData/TestCD-alzkdocqjhuaigahqlichpzxvfvo/Build/Intermediates/TestCD.build/Debug-iphonesimulator/TestCD.build/DerivedSources/CoreDataGenerated/Model/Photo+CoreDataClass.swift’
This is saying that there are two files called Photo+CoreDataClass.swift, both are being compiled and hence the Photo CoreData Entity is being defined twice.
One workaround that works for me is to:
Delete the files Photo+CoreDataClass.swift and Photo+CoreDataProperties.swift
Remove them from the Project and Move to Trash
Do a Product -> Clean from the main menu
Regenerate the Photo Entity class files with Editor -> Create NSManagedObjectSubclass (whilst in the CoreData model)
Build/run
However, this bug is sneaky, and sometimes you just have to keep playing with it. What is happening is that sometimes Xcode is generating the files for you during the Build phase, even when you don’t want it to. And when the files are already part of your project, then you have two identical files being compiled into the project.
In Xcode 8 beta if Tools Version — Minimum in the File inspector of the .xcdatamodeld file is “Automatic (Xcode 8.0)”, any changes you make to your data model will eventually disappear (for example, after opening a different file in the tab where you edited the data model and then immediately going back).
The workaround is to select “Xcode 7.3” or older as the minimum version, make changes to your data model, build the project (optionally save and close the project, open it again and make sure everything is as it is supposed to be), and finally change the minimum version back to “Automatic (Xcode 8.0)”, otherwise code generation for NSManagedObject subclasses won’t work properly. Now you can create instances of the automatically generated NSManagedObject subclasses in your project.
If changing the minimum version in Xcode doesn’t help, you can try closing the project and directly editing the contents of .xcdatamodel file located inside .xcdatamodeld file whenever you need it (look for minimumToolsVersion property on the second line of the file). As always cleaning and building project should help if anything goes wrong.
Deleting the files, cleaning, and re-generating worked for me.
But I had a different problem before I got to that point. I was getting “‘Photo’ is ambiguous for type lookup in this context” error
I used the solution here:
The fix is to select the Photo model and, in the attributes inspector, set the “Class -> module” to “Current Product Module” and “Class -> codegen” to “Manual/None”. I then change the Class-> Module back to blank and it stopped giving me that error.
I am finally getting comfortable using SO, this, and other forums to solve iOS problems, yay
I was stuck with the same error. Tried a few things before I got something to work. I haven’t continued on much further, so this may not be the best workaround, but I deleted both generated files which were apparently duplicated, then built/ran the app without them. I’m getting yellow caution errors that those files are missing, but Xcode auto-generates and the app works fine in simulator.
It seems like Xcode doesn’t save the Codeine property.