简体   繁体   中英

iOS Core Data Abstract and Child Classes

I'm working on adding CoreData to an existing iOS project, and am having problems with data showing up in my child classes that inherit from an abstract class.

I have an abstract class ( Object ) that defines a shared set of fields for my desired child entity/concrete classes. The abstract class contains title , startDate , for example. It's setup in the data model to have a parent entity of Object . Object is setup as an abstract class in the data model.

My child entity ( Expense ) has a property amount (among others).

I typically would begin creating my Expense as so:

Expense *expense = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Expense class]) inManagedObjectContext:[Store sharedStore].managedObjectContext];

Then set my desired properties:

expense.amount = [NSDecimalNumber decimalNumberWithString:amountString];
expense.title = title;
expense.startDate = [NSDate dateFromString:dateString withFormat:@"yyyy-MM-dd"];
(other properties on expense set here)
.
.

I'd then call save on my context:

[[Store sharedStore].managedObjectContext save:&error];

This returns YES for being stored to CoreData.

HOWEVER , when I try to LOG the expense's amount (or any of its other set properties), they come out as nil, 0, etc. depending on the property type.

I have other base entities/models in my project saving fine in this fashion, so it's nothing to do with my CoreData setup or Store class handling the object context. Specific problem arose with the abstract class.

How do I setup my child entities of my abstract class in a way that it will correctly save the properties on that child class? I'm spinning my wheels and can't seem to find a good example of how to do so in Apple docs or anywhere online. Anything is appreciated. :)

After much debugging, and directly contacting @PhillipMills, we were able to land on the problem, and I hope others don't have to spend time pulling their hair out. :)

Having - (void)setAmount and - (void)setPayee methods on the Object class were causing the desired setters to be overridden. I didn't intentionally implement these, but they existed on my Object class before I introduced CoreData into my project.

Removing those getters and setters on the Object class made things work as expected. Much thanks to Phillip for his help!

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