简体   繁体   中英

Adding an Attribute to a Core Data Entity

this has beed discussed alot, but nothing that I found helped me... I just want to add a single attribute to a singe entity. Of cause without loosing my data.

To do that, I followed these steps

But I don't think it worked at all. There are no errors as long as not try to use the new attribute. But in my Core Data Model (when I open the .xcdatamodel in XCode) it's there and even in my NSManagedObject Subclass of the entity, the new attribute is there as a property. But when I try to use it, this happens:

unrecognized selector sent to instance 0x74f8520

or

the entity Name is not key value coding-compliant for the key "isFavoriteName".'

The key "isFavoriteName"is the one I just added.Like I said, I did all the steps. Do I miss something? I think, that the new Model Version is not being used, because when I delete some attributes in the Model, my app still works and the deleted information are still shown... I have set the current Core Data Model Version the the new one (Step 6).

I hope you can help me!

Nick

EDIT: Do I may have to change something in code as well? Apart from the setting the options when creating persistentStoreCoordinator... Because I have, let's say myApp.xdatamodel and inside of that, i know have myApp.xdatamodel and myApp 2,xdatamodel... but I my code I sill do:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myApp" withExtension:@"mom"];

_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

That's right-- the page you linked to left out one step that applies in your case. Even though you created a new version of the model, your code is still loading the old one by name.

Don't just change it to load version 2 though. Do this instead:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myApp" withExtension:@"momd"];

This will look in the momd folder and get whatever the current version is. In this case it will get version 2. If you change the model again in the future, it will get whatever version is current then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM