30

I have an Objective-C model class MyType. This class is used in Swift code:

NSEntityDescription.insertNewObjectForEntityForName("MyType", inManagedObjectContext: context) as! MyType

The as! cast results in the error message

Core Data: Could not cast value of type 'MyType_MyType_2' (0x7be99a30) to MyType (0xf33db74).

If I cast it as NSManagedObject it works. When I print the result, I can nevertheless see, that it is an actual instance of MyType:

<MyType: 0x7ae06d50> (entity: MyType; id: 0x7aeba6d0 <x-coredata:///MyType/t957F2860-85F8-46E0-B6D6-4D1DF6C4EC613> ; data: { 
    ...the fields...
})

What is happening here? And where does the name MyType_MyType_2 come from?

Bastian
  • 4,248
  • 5
  • 34
  • 53
  • 1
    Have you add `@objc(MyType)` before MyType class declaration ? – Zell B. Aug 17 '15 at 11:59
  • Nope, as I said, the model MyType is already Objective-C code. – Bastian Aug 17 '15 at 12:01
  • 1
    I have same problem, works fine in project and other targets but is not working on my test target. Moreover it was working, after an XCode update do not know which version (currently on 8.3.2) it stopped. I am unable to run my tests due to this crash. Any idea how to resolve it? Suggestion from below do not helps :( – Shial May 24 '17 at 03:00

4 Answers4

44

When I had this issue, it was because I had forgotten to set the "class" on the entity. This is what I came up with:

  1. Click on .xcdatamodelId file in your file structure/project navigator pane (far left).
  2. Select the entity that you are having issues with.
  3. In the Utilities pane (far right), look for the icon that looks like a 1997 cell phone, the Data Model Inspector.
  4. Under the entity settings, you should see two fields, "Name" and "Class" - set up "Class" with the name of the class you are using (typically the same as "Name").

You'll even notice before you follow these steps that the default "class" is NSObject, reflecting the error message. I found some programatic ways to do this too, but this seemed like the simplest/quickest solution.

I should note that my model WAS written in Swift, so I did have to add the @objc(Entity) interoperability reference mentioned by @zellb. But that shouldn't make a difference in the solution as long as you are doing that part properly (and that would cause a different unrelated error from my understanding).

Bourne
  • 2,219
  • 1
  • 17
  • 27
39
  1. Set Entity Class Name
  2. Set Module "Current Product Module" like below

enter image description here

Cody
  • 3,910
  • 3
  • 34
  • 39
  • I started my Swift project in xCode 7 and entities I created there had `Module` specified as shown above. Now in xCode 8 (8A218a) `Module` is empty after creating a new entity. Specifying as above fixed it for me. – Murray Sagal Oct 25 '16 at 19:34
  • Unfortunately, this causes https://stackoverflow.com/q/39931137/2740693 when I'm using Objective-C CoreData. Any insights? – Christian Di Lorenzo Mar 17 '18 at 03:33
  • I had to put the class name as well and just adding: press cmd + k (clean) and build cmd + b again. Run your project. It worked for me. thanks! – Dasoga Oct 02 '18 at 04:34
7

just try this:

@objc(MyType)
public class MyType: NSManagedObject {
    // your class
}

instead of this:

class MyType: NSManagedObject {
    // your class
}
Wajih
  • 3,577
  • 2
  • 21
  • 37
0

I had mistakenly set a "parent entity" in one of my entities in the data model inspector in the entity section. I mistakenly thought that referred to the destination of a one-to-many relationship.

Setting it back to "no parent entity" fixed the problem, although I did have to delete and reinstall the app in the simulator to deal with the messed up core data database.

Darrell Root
  • 548
  • 4
  • 16