Search code examples
objective-ccocoapolymorphismnsarraycontrollermaster-detail

NSArrayController for polymorphic class


I have the following (stripped down) class interfaces:

@interface ScriptEvent : NSObject {
  ...
}

@interface SingleLine : ScriptEvent {
  NSString *line;
}

@interface MultiLine : ScriptEvent {
  NSArray *lines;
}

Another parent class holds an NSArray containing a list of ScriptEvents (which will either be SingleLine or MultiLine).

In my XIB I have an NSArrayController bound to this list of ScriptEvents and I want to set up a master/detail arrangement. So I have an NSTableView linking to this NSArrayController and I want to show a different detail panel depending on whether the selected member of the NSArrayController is a SingleLine or a MultiLine.

Is this possible?


Solution

  • Check if the selected member is a SingleLine or a MultiLine with:

    if([objectToCheck isKindOfClass:[SingleLine class]]){
        //Do some staff
    }else if([objectToCheck isKindOfClass:[MultiLine class]]){
        //
    }else{
        //
    }