I think the big difference is the way the method is called!
If you do not define a protocol, you need to use the performSelector: method to call the unknown method. It works, but in this case we are limited by the number and type of parameters of your method. In fact, you cannot give a non object parameter, and you can pass no more than two parameters using performSelector:withObject:withObject: If you need more arguments, you need to use an array or a dictionary.
id<MyProtocol> myObj = [[MyClass alloc] init]; if([myObj respondsToSelector:@selector(aMethod:)]){ NSArray *args = [NSArray arrayWithObjects:@"First arg",[NSNumber numberWithInt:2],[NSNumber numberWithBool:YES],nil]; [myObject performSelector:(@selector(aMethod:) withObject:args]; }
Otherwise, if you define the protocol using your optional method, you simply call it normally:
id<MyProtocol> myObj = [[MyClass alloc] init]; if([myObj respondsToSelector:@selector(aMethodWithFirstArg:second:third:)]){ [myObject aMethodWithFirstArg:@"First arg" second:2 third:YES]; }
The same result, but I prefer the second with the protocol!
source share