6

I'm trying to use the same core data model in my app as well as an extension, but am unsure how to share the core data model between the 2. The class for the core data model uses a namespace with the classname, so when I try and fetch the objects in the extension I get the unable to load class named "" error.

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

Is there anyway to not use a namespace in the classname, or is there a way to make the extension inherit the namespace of the main project?

Core Data Entity

Stephen Donnell
  • 810
  • 9
  • 20

1 Answers1

10

I was stuck on this earlier. Seems like a real issue, and it wouldn't hurt to file it as a feature request/bug with Apple.

In the meantime, you can get around it with two steps. First, tag your NSManagedObject subclasses with @objc(ClassName). Simply insert it above the class declaration:

@objc(ClassName)
class ClassName: NSManagedObjectSubclass {
    @NSManaged var name : String
}

Second, go back to the managed object model, and remove the namespace from the "class" name field in the inspector for the Entity you're working with.

This worked for me today, after reading this: I can't use my core data model in two targets in a Swift project

Community
  • 1
  • 1
Steven Hovater
  • 1,389
  • 2
  • 12
  • 25
  • 1
    This worked for me as well. Also remember to clean after you've done the changes. – mokagio Dec 25 '14 at 05:46
  • Just a confirmation: when you say "go back to the managed object model, and remove the namespace from the "class" name field" you mean that, if before that field contained "myApp.Entity", now should only be "Entity"? – cdf1982 Jul 24 '15 at 10:02
  • 1
    Yes...I should also add that I recently got a note that Apple thinks they've fixed this in Xcode 7, which is in Beta now. I haven't tested it yet, though. – Steven Hovater Jul 24 '15 at 12:41