Cyclic Reference - protocols and subclasses

Posted by blindJesse on Stack Overflow See other posts from Stack Overflow or by blindJesse
Published on 2010-01-28T07:09:14Z Indexed on 2010/05/27 23:01 UTC
Read the original article Hit count: 300

Filed under:

I'm getting some cyclic reference (I think) problems between a few classes that require imported headers due to either subclassing or protocol definitions. I can explain why things are set up this way but I'm not sure it's essential. Basically these classes are managing reciprocal to-many data relationships.

The layout is this:
Class A imports Class B because it's a delegate of Class B and needs its protocol definition.
Class B imports Class C because it's a subclass of Class C.
Class C imports Class A because it's a delegate of Class A and needs its protocol definition.

Here's some sample code that illustrates the problem. The errors I'm getting are as follows: In Class A - "Can't find protocol definition for Class_B_Delegate". In Class B - "Can't find interface declaration for Class C - superclass of Class B." In Class C - "Can't find protocol definition for Class_A_Delegate".

Class A header:

#import <Foundation/Foundation.h>
#import "Class_B.h"

@protocol Class_A_Delegate
@end

@interface Class_A : NSObject <Class_B_Delegate> {
}

@end

Class B header:

#import <Foundation/Foundation.h>
#import "Class_C.h"

@protocol Class_B_Delegate <NSObject>
@end

@interface Class_B : Class_C {
}

@end

Class C Header:

#import <Foundation/Foundation.h>
#import "Class_A.h"

@interface Class_C : NSObject <Class_A_Delegate> {
}

@end

© Stack Overflow or respective owner

Related posts about objective-c