insertNewObjectForEntityForName returns wrong type
I've been pulling my hair for the last few hours trying to figure out why insertNewObjectForEntityForName
is returning NSManagedObject
even though I'm trying to insert an entity of my own type.
Things I have checked a thousand times:
- Class name is set to the correct value in the diagram designer
- Entity has many-to-many relationship with another entity, creating that other entity works as expected and I do get the properly typed output, I swear they are identical
What are the possible reasons why this could be hap开发者_JAVA百科pening? I don't know where to look from here, and google didn't help much :(
EDIT (RESOLVED):
This had to do with versioning... I removed the data model and recreated it again, and everything started working, before there were a few versions that were previously used... sigh sometimes asking a question gives an idea of how to find a solution
I'm still confused of why that was happening, even though it's solved now
I ended up in a similar situation with a different solution. Through a series of unfortunate events (a namespace clash forced changing one of my entities) I ended up with a custom entity that had its Class set to the default of NSManagedObject in the data model rather than the proper custom subclass.
So before you go through the hassle of deleting your entire data model and rebuilding it (as I just did, thank god for git branching), just check to make sure your Class is still set properly.
I had this happen as well but it wasn't a corrupt model it was actual a typo in my entity definition in the model file.
Basically the entities and attributes were all correct but the "ClassName" for my entity had a typo. This cause Core Data to have enough info to actually read and write the entity but it was not able to instantiate the class (since it didn't actually exist with the name I had mistyped)
This had to do with versioning... I removed the data model and recreated it again, and everything started working, before there were a few versions that were previously used... sigh sometimes asking a question gives an idea of how to find a solution
I got same situation today after deleting some of source files of NSManagedObject subclasses and regenerating them from the Model Editor in Xcode.
I've examined the project.pbxproj file, and found that there are some inconsistent id-source file pairs (possibly Xcode bug?).
I've managed to fix it by removing all the models from the project and add them again (and I also removed some of the file links from the project.pbxproj manually).
Thank you for the hint.
Have the same problem today. After 2 hours of hair pulling found that I just don't add entity class source file into target!
精彩评论