An NSManagedObject of class 'Location' must have a valid NSEntityDescription

I’ve gotten to page 158 and I am unable to get the MyLocations app to run after adding Core Data support.

Stepping through the code in LocationDetailsViewController.swift done()'s the line “let location = Location(context: managedObjectContext)” cause the following error:

Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘An NSManagedObject of class ‘Location’ must have a valid NSEntityDescription.’

Xcode opens AppDelegate.swift and shows execution stops at “class AppDelegate: UIResponder, UIApplicationDelegate {”.

I’ve done extensive diff’ing of the code to make sure I’m current and I appear to be. I also opened “03 - Core Data” and it runs correctly. Not sure why my project won’t run.

It sounds like in your DataModel where you created the Core Data entity, the entity does not have the right name or the right class name. Double-check the inspector panes to see everything is set up properly.

Also, make sure the DataModel file actually gets included into the project (you can see this in the File inspector pane).

I’ve narrowed the issue down and have an A/B working/not-working case. The offending line is in the generated Location+CoreDataClass.swift file (Xcode version 8.2.1 (8C1002)). Your code in the zip file doesn’t have offending line. After commenting out the following line of code shown below allows my code to work.

public class Location: NSManagedObject {

Same issue as by gerrior. Come to page 158. Xcode version 8.2.1. All files are included into the project.
Fixing (removing) line //@objc(Location) was not enough.
After digging to the original source from the book and comparing Core Data related sources I have found one more issue in DataModel.xcdatamodeld file
On the CONFIGURATIONS > Default page for Location Entity the class name was missing (my Xcode didn’t generate it).
After inserting ‘.Location’ as in sources from hollance the error has disappeared.
Hope it will help for other guys with same problem.

1 Like