CoreData & Subclasses

Discussion in 'Mac Programming' started by AlJaMa, Aug 17, 2009.

  1. AlJaMa macrumors newbie

    May 12, 2008
    Hey everyone,
    I am trying to wrap my head around a seemingly simple concept. I would like to use CoreData to create a listing of different objects that all spawn from the same parent class. Here is example of my data Schema. [​IMG]

    What I would like to do is create a list of all Events (with just data from the "Event" parent Class) but then when the item is clicked I can display the data that is specific to that child class.

    Currently I have the list working, all I need now is a way to determine the type of the subclass of Event. If This is not clear enough, here is how I am running it currently.

    Create "partyEvent"
    List all "Event" objects
    When clicked, show detail of the "PartyEvent" object

    What I was thinking is that I could add a "type" to the "Event" class, then just cast accordingly when it is clicked?

  2. MrFusion macrumors 6502a

    Jun 8, 2005
    You don't need to cast. Just use "id". If you have to find out which type it is, get the entitydescription of the managedobject.

    (untested code)
    -(void) someMethod {
     id yourEvent = [self getEvent]; //I assume you have an instance somehow.
     NSString *type = [[yourEvent entity] name];
     [yourEvent valueForKey:@"latitude"]; //possible for all events
     if ([type isEqualto:@"PartyEvent"]
         [yourEvent valueForKey:@"extradata"]; //only for some events
    Maybe you need to go over the structure of your model again. I would make the relationship from event to photo a "to many". Or can you not have multiple photo's for a single event?
  3. AlJaMa thread starter macrumors newbie

    May 12, 2008

    Wow thanks so much, I cant believe that I forgot about id :eek: haha. Anyways, thanks for noticing the error in the data structure too, but I just made that one up real quick as to not over complicate the example.

    Thanks again!

Share This Page