I’m trying to write a custom, formal protocol and I am having issues with Xcode’s warnings. Specifically:
if([self.delegate conformsToProtocol:@protocol(myProtocol)]){
[self.delegate myProtocolMethod];
}
This works totally fine at runtime, but Xcode keeps giving me the “NSObject may not respond to -myProtocolMethod” warning. I’d really like to remove the warning, so am I doing something wrong here?
The compiler doesn’t care if you just checked for the protocol. It only cares about the data type of the object you are calling the method on. The easiest thing to do is simply cast the result of
self.delegateto a type which declares the protocol.Or, if the delegate property is supposed to always implement this protocol, you can change the property declaration to include it. This is even better because the compiler will warn you if you try to assign an object which doesn’t implement the protocol as the delegate.