When working on complex applications, you occasionally find yourself writing code that looks like this:
#import "B.h" |
@protocol A |
- foo:(id <B>)anObject; |
@end |
where protocol B is declared like this:
#import "A.h" |
@protocol B |
- bar:(id <A>)anObject; |
@end |
In such a situation, circularity results and neither file will compile correctly. To break this recursive cycle, you must use the @protocol directive to make a forward reference to the needed protocol instead of importing the interface file where the protocol is defined. The following code excerpt illustrates how you would do this:
@protocol B; |
@protocol A |
- foo:(id <B>)anObject; |
@end |
Note that using the @protocol directive in this manner simply informs the compiler that “B” is a protocol to be defined later. It doesn’t import the interface file where protocol B is defined.
Last updated: 2008-02-05