CoreData Abstract entity with relationship

这一生的挚爱 提交于 2019-12-11 11:19:21

问题


I have this situation:

I have two CoreData entities:

MUSIC_ACTIVITY is an ABSTRACT CoreData entity;

LOCATION: with some attributes as: city, address,...

BAR: with some attributes; PUB: with some attributes;

in MUSIC_ACTIVITY I have a relationship with LOCATION (1to1).

MUSIC_ACTIVITY is the PARENT entity of BAR; MUSIC_ACTIVITY is the PARENT entity of PUB;

I generate automatically class of this model.. So: In MUSIC_ACTIVITY I have one property LOCATION In LOCATION I have property MUSIC_ACTIVITY (inverse)

How can I access to LOCATION information into BAR and PUB entity ?

for example: I declarated the relationship with LOCATION using 'address' (like name). So in MUSIC_ACTIVITY I have a property address (LOCATION type). In PUB I need to overwrite the init..for example:

-(id) initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context
{
    self = [super initWithEntity:entity insertIntoManagedObjectContext:context];
    if (self) {
        self.name = @"MY NAME";
        self.address = .... ///ERROR ////
    }
    return self;
}

but I can't use self.address ...


回答1:


You can access parent entity's attributes the same as you would the properties of a parent class.

Just some debugging tips for entity inheritance in XCode:

  • Double check the parent entity
  • Change the outline style of the xcdatamodel viewer to hierarchical

I also recommend MOGenerator. You don't have to manually recreate the backing class definitions when you change the model.



来源:https://stackoverflow.com/questions/25703485/coredata-abstract-entity-with-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!