14

I have a problem with core data in IOS 8. Whenever I want to use the insertNewObjectForEntityForName method, I get the

Class not found, using default NSManagedObject instead.

Error message. I use objective-c, and I did not had this problem using IOS 7. Also could there be any problem because in my workspace there are two projects, one containing the core data relating code, and the other the UI.

Dániel Nagy
  • 10,907
  • 7
  • 44
  • 56

6 Answers6

31

When using Swift the error could still appear also if the source file is included in the Build Phases > Compile Sources the error

unable to load class named '...' for entity '...'. class not found, using default nsmanagedobject instead.

To solve this you could prefix the specific model class in the model inspector (here with MyApp). See also the documentation.

enter image description here

Our you can add objc(Person) before your swift class declaration

@objc(Person)
class Person: NSManagedObject {
   ...
}
H6.
  • 26,822
  • 12
  • 70
  • 78
  • @objc() also did the trick for me. But, so did removing the "class" portion and just keeping "name" which may be a better solution for some people. Thank you High6 – anders Oct 16 '15 at 19:40
  • Xcode 12.4 will automatically remove the '.' when you press Enter. – William Grand Apr 12 '21 at 15:55
29

This is an updated version from the answer provided from @High6

As of Xcode 7 there is a Module property that you can set to Current Product Module (it appears automatically), so you don't need to use prefixes.

Xcode Core Data editor

FranMowinckel
  • 3,617
  • 24
  • 25
15

There is a similar answer to this question. In my case I was using Objective-C and doing this got rid of this error in the console.

I found the answer here: Unable to find specific subclass of NSManagedObject

You can empty the "Module" field (it will show "None") and mark the managed object subclasses with @objc(classname)

enter image description here This fixed it for me.

Community
  • 1
  • 1
C0D3
  • 6,127
  • 7
  • 40
  • 66
14

That error means that you've configured a custom class name for the entity type in the model editor, but that the class does not exist at run time. Core Data falls back on creating a generic NSManagedObject using the entity type.

Assuming that the class file actually exists somewhere, the problem is that you're not including that file in the app target in Xcode. Since Xcode supports multiple targets, projects, etc, it doesn't automatically include every source file in every build. Add the Core Data subclass file(s) to the app target and this error should go away.

Tom Harrington
  • 62,792
  • 9
  • 129
  • 149
  • It worked, I copied both the .h and .m files to the compile sources, and now there is no warning. – Dániel Nagy Sep 17 '14 at 08:09
  • 3
    For me it was because I had created an `NSManagedObject` subclass, but auto generator didn't wrap the class name in `@objc` (even though my project is in Swift). Eg, for above the class declaration for a `Temperature`, add `@objc(Temperature)` – PostCodeism May 21 '19 at 19:44
  • It can't be for XCtest cases. – JBarros35 Feb 28 '20 at 15:27
  • It will leads to another error upfront. We should have a way to avoid adding these things on the test target. – JBarros35 Mar 02 '20 at 10:22
  • File an issue with Apple and hope for the best. They don't read this site, so tell them directly. – Tom Harrington Mar 02 '20 at 18:51
9

If you are using Xcode 7 beta the actual solution is removing the @objc(Person). Must be a bug in the template that is used for generating entity classes in Xcode 7 beta.

Samuel Mellert
  • 659
  • 1
  • 7
  • 12
  • This also worked for me. Swift is such a dynamically-changing language. Someday, it'll settle down... – James Perih May 20 '16 at 20:15
  • I'm having the problem after a migration which adds an entity to the model. Core Data seems unable to see the newly created class related to the new entity. Removing objc() in the new class worked for me... however now in the project I have the new class with objc and all other old classes which don't have objc (the old classes still work ). – Andrea Gorrieri Jan 18 '19 at 10:57
0

When none of the solutions worked for me, I deleted the existing NSManagedObject and created a new one. If you need any code from the previous object remember to take a backup, and later copy that into the new NSManagedObject class. Hope this helps someone.

Mot
  • 61
  • 8