Checking to see if an optional protocol method has been implemented Checking to see if an optional protocol method has been implemented objective-c objective-c

Checking to see if an optional protocol method has been implemented


respondsToSelector: is part of the NSObject protocol. Including NSObject in MyProtocol should solve your problem:

@protocol MyProtocol <NSObject>@optional-(void)optionalProtocolMethod:(id)anObject;@end


What I do is applying the following recipe:

if(self.delegate && [self.delegate respondsToSelector:@selector(closed)]){    [self.delegate closed];}

Where 'closed' is the method that I wanted to call.