Reputation: 19291
The documentation for class_addProtocol
is very basic and doesn't really explain how to use it.
My interpretation is that when I want to add a protocol to a class at runtime I should call class_addMethod
to add each method from the protocol and then call class_addProtocol
. What I don't really understand is if and why I need to call class_addProtocol
. Surely by adding all the protocol methods the class already conforms to the protocol, so what does class_addProtocol
actually do?
Upvotes: 3
Views: 736
Reputation: 55573
It allows for the selector -conformsToProtocol:
to work. In some situations (where you may have multiple delegates, who may need to respond to certain things but not others), it's useful to know if the object responds to the protocol, instead of just the single method you are requesting.
Most of the time this isn't an issue, as most of the time -respondsToSelector:
is sufficient, but it's a good tool to have handy.
Note that you should almost never use class_conformsToProtocol
over -conformsToProtocol
, as that doesn't check superclasses, and can cause issues.
Upvotes: 3