CoreData具有关系的抽象实体 [英] CoreData Abstract entity with relationship

查看:421
本文介绍了CoreData具有关系的抽象实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况:

我有两个CoreData实体:

I have two CoreData entities:

MUSIC_ACTIVITY 是一个摘要CoreData实体;

MUSIC_ACTIVITY is an ABSTRACT CoreData entity;

LOCATION :具有以下属性:city,address,...

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

BAR :带有一些属性;
PUB :有一些属性;

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

MUSIC_ACTIVITY (1to1)。

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

MUSIC_ACTIVITY BAR 的PARENT实体;
MUSIC_ACTIVITY PUB 的PARENT实体;

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


在:
MUSIC_ACTIVITY 我有一个属性 LOCATION
LOCATION MUSIC_ACTIVITY (反)

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

如何在 BAR PUB 实体?

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

例如:我使用address(如name)声明了与LOCATION的关系。所以在MUSIC_ACTIVITY我有一个属性地址(LOCATION类型)。
在PUB我需要覆盖init..for示例:

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;
}

但我不能使用self.address ...

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

推荐答案

您可以像父类的属性一样访问父实体的属性。

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

只是一些XCode中实体继承的调试提示:

Just some debugging tips for entity inheritance in XCode:


  • 仔细检查父实体

  • 将xcdatamodel查看器的大纲样式更改为层次结构

我还建议使用MOGenerator。在更改模型时,不必手动重新创建支持类定义。

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

这篇关于CoreData具有关系的抽象实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆