Jul 22

The @optional tag in a protocol will define methods that can optionally be implemented. It is important when using a protocol (as a delegate or similar) to check that the implementing protocol has implemented the optional method before messaging it (using respondsToSelector:).

@protocol MyProtocol
@optional
// implementation of these methods is optional
- (void)anOptionalMethod;
@required
// implementation of this method is required
- (void)aRequiredMethod;
@end

Before using it you would do the following:

if ([delegate instancesRespondToSelector:@selector(aMethod)]) {
[delegate aMethod];
}

Comments are closed.

preload preload preload