views:

72

answers:

1

I am assigning protocols in a couple classes that follow an inheritance tree. Like so:

first class

@protocol LevelOne
- (void) functionA
@end

@interface BaseClass : NSObject <LevelOne> {
}

second class

@protocol LevelTwo <LevelOne>
- (void) functionB
@end

@interface SubClass : BaseClass <LevelTwo> {
}

Later I am assigning the class as delegate properties of other classes

base class

@interface AppClass : NSObject {
    @protected
    id<LevelOne> levelOneDelegate;
}

@property (assign) id<LevelOne> levelOneDelegate;

subclass

@interface AppClassesFriend : AppClass {
    @protected
    id<LevelTwo> levelTwoDelegate;
}

@property (assign) id<LevelTwo> levelTwoDelegate;

At the end of this journey, AppClassesFriend has 2 properties on it.

"levelOneDelegate" has access to "functionA", when it is assigned with a BaseClass object.

However, I am finding that "levelTwoDelegate" only has access to "functionB" (it is assigned with a SubClass object).

In order to have AppClassesFriend be able to use both functions, it seems I need to assign BOTH a levelOneDelegate AND levelTwoDelegate.

Is there any way to make "levelTwoDelegate" have access to both? Since, both functions are available on "SubClass".

So, what I would like to be able to do is :

SubClass *s = [SubClass alloc];
AppClassesFriend *a = [AppClassesFriend alloc];
a.levelTwoDelegate = s;

so inside AppClassesFriend (a) I could use :

 [self.levelTwoDelegate functionA];   <---- this is never found
 [self.levelTwoDelegate functionB];

but it seems I have to add

a.levelOneDelegate = s;

Thanks if anyone took the time to read all the way down this far. So in summary the question is, how do I get "levelTwoDelegate" to have access to both functionA and functionB?

+1  A: 

Simply declare that your subclass's delegate property implements both level one and level two protocols (i.e. implements both functionA and functionB):

@interface AppClassesFriend : AppClass {
    @protected
    id<LevelOne,LevelTwo> levelOneAndTwoDelegate;
}

@property (assign) id<LevelOne,LevelTwo> levelOneAndTwoDelegate;
hatfinch
thanks - that is correct although my problem had an additional facet it seems!Since the superclasses are coded to used levelOneDelegate ONLY, any non-override methods default to using that code.Its not possible to reassign levelOneDelegate in a subclass to include additional protocols, so either the base class needs to be aware of ALL protocols (no way, too sloppy) or, I have to assign the receiver to both delegates it seems.
Michael
It's not possible to reassign levelOneDelegate, but you can make an additional delegate type which overrides your original one. For instance, UITextView does this so that its delegate implements UITextViewDelegate rather than UIScrollViewDelegate. Simply declare functionA in SuperclassDelegate and functionA and functionB in SubclassDelegate.
hatfinch