Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Referring to Other Protocols

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.



< Previous PageNext Page > Hide TOC


Last updated: 2008-02-05




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice