Xcode 7 + Swift 2 + CoreData = "Unable to load class"

I've been toying with the beta of XCode 7 over the past week or so, and came across a rather annoying problem with generated NSManagedObject subclasses. Luckily, there's a simple fix, but it took some tracking down.

After setting up my data model and generating instances for my first Entity, I would find myself seeing the following error in the console whenever I tried to create an instance of the entity:

CoreData: warning: Unable to load class named 'MyProject.Entity' for entity 'Entity'.  Class not found, using default NSManagedObject instead.

And worse than just getting a warning, none of my NSManagedObject instances persisted.

It turns out this is a result of the default name for the entity class in the model configuration, which was generated along with the corresponding NSManagedObject subclass:



Note the dot in front of the class name. This is inserted for the sake of namespacing, which doesn't exist in Swift. As a quick fix for this, I just removed the dot to refer to the class explicitly.

This got rid of the annoying warning, and my entities persisted as expected.

Since XCode 7 (and indeed, Swift 2) are still in beta, there are going to be a number of changes to come, so this problem may go away, or the behavior may be altered so this fix no longer works (or breaks things that might otherwise work). So I'm going to keep a close eye on this one and update this post should anything change.