Check if a method exists
if ([obj respondsToSelector:@selector(methodName:withEtc:)]) {
[obj methodName:123 withEtc:456];
}
There is also the static message instancesRespondToSelector:(SEL)selector You would call it like this:
[MyClass instancesRespondToSelector:@selector(someMethod:withParams:)]
or like this:
[[myObject class] instancesRespondToSelector:@selector(someMethod:withParams:)]
This may be useful if you would like to call one constructor or another one depending on this (I mean, before having the instance itself).
Use respondsToSelector:
. From the documentation:
respondsToSelector:
Returns a Boolean value that indicates whether the receiver implements or inherits a method that can respond to a specified message.
- (BOOL)respondsToSelector:(SEL)aSelector
Parameters
aSelector - A selector that identifies a message.Return Value
YES
if the receiver implements or inherits a method that can respond to aSelector, otherwiseNO
.