delegate declared in interface/protocol?
could you help me understand something with the delegates and protocols :
in this code :@protocol FirstViewControllerDelegate; @interface FirstViewController : UIVIewController { … id<FirstViewControllerDelegate> delegate; } @property (assign) id <FirstViewControllerDelegate> delegate;
- what does mean the first line "@protocol" ? i don't see in my project this delegate file, and the current file (with @interface) seems to be a "simple" interface for my FirstViewController, so i'm a bit confused.开发者_Go百科
- i've seen how to use the delegate pattern, but i've never seen that we need to "declare" the delegate in the .h, is it something that must be written in the .h file (i'm talking about id < .... > delegate) ?
Thanks for your answer
Paul
The
@protocol FirstViewControllerDelegate;
in that format is a forward declaration. It tells the compiler that FirstViewControllerDelegate
is a valid protocol that will be defined later on (sometimes just further down in the same .h file). It is required because without it the compiler will complain when it sees the line
id<FirstViewControllerDelegate>
as it has not seen its declaration.
The actual protocol may be defined something like
@protocol FirstViewControllerDelegate
{
@required
- (void)myImportantDelegateMethod;
}
精彩评论