If you want to call the protocol method for a delegate object that, I hope, implements the corresponding protocol method, I see that the developers first check
if([delegate respondsToSelector: @selector(aMethod)]) {
Is it not better or even safer to do this?
if([delegate conformsToProtocol:@protocol(MyProtocol)] && [delegate respondsToSelector: @selector(aMethod)]) {
I know that if the protocol method definitions were properly structured, the delegate should never have conflicts or implementations that cannot be intended for / from MyProtocol. Such conflicts are far away, but I came across a protocol method definition that is simply declared as - (void) willStartLogin ;. I am sure that you can already start thinking and suggesting how such a protocol method is bad, it can, for example, be implemented by a delegate for personal / internal use, and not for use in accordance with the myDelegate protocol. It would be better to declare the MyProtocol method like this: - (void) myObjectWillStartLogin: (MyObject *) myObjectInstance; to get rid of any ambiguity and make things obvious.
I hope that I will not miss anything that forces me to check only the responses to the request: Thank you
objective-c protocols delegates selector respondstoselector
pnizzle
source share