Reputation: 20115
I am looking at Apple's sample code for lazy table image loading. I see that they have two lines starting with @protocol ParseOperationDelegate
. Why do they do this twice? All the documentation on Objective C protocols I've looked at do not tell you to do it twice.
@class AppRecord;
@protocol ParseOperationDelegate;
//@interface ParseOperation : NSOperation <NSXMLParserDelegate>
@interface ParseOperation : NSOperation
{
@private
id <ParseOperationDelegate> delegate;
NSData *dataToParse;
NSMutableArray *workingArray;
//AppRecord *workingEntry;
//NSMutableString *workingPropertyString;
//NSArray *elementsToParse;
//BOOL storingCharacterData;
}
- (id)initWithData:(NSData *)data delegate:(id <ParseOperationDelegate>)theDelegate;
@end
@protocol ParseOperationDelegate
- (void)didFinishParsing:(NSArray *)appList;
- (void)parseErrorOccurred:(NSError *)error;
@end
Upvotes: 1
Views: 408
Reputation: 54836
The @protocol ParseOperationDelegate;
line is not defining the protocol. It's a forward declaration. Basically it's saying that "a protocol called ParseOperationDelegate
exists and is defined somewhere else in the code".
They do this so that the compiler will not die with an error on the line that goes id <ParseOperationDelegate> delegate;
. The alternative would be to have the entire protocol definition placed before the interface definition (which personally I think would be a better solution in this case).
In such a simple case as this one I think there is little point in having the forward declaration. But you can easily imagine a more complex case where perhaps the protocol definition lives in its own header file (or in the header file for some other class). In a case like that, using a forward declaration allows you to avoid having to #import
the protocol's header file into your class's header. It's a small difference, really, but it can be useful.
Upvotes: 6