How to find out if a method exists in Obj-C when given an NSString
Posted
by Justin
on Stack Overflow
See other posts from Stack Overflow
or by Justin
Published on 2010-05-03T21:00:08Z
Indexed on
2010/05/03
21:18 UTC
Read the original article
Hit count: 196
I have a method as an NSString *
. If it exists, I want to call it, and if not, do nothing.
SEL eventSelector = NSSelectorFromString(eventSelectorStr);
if ([delegate respondsToSelector:eventSelector]) {
[delegate performSelector:eventSelector];
[delegate adapterDidFinishAdRequest:self];
}
else {
// Does not implement selector
}
This code does not work, since NSSelectorFromString
will register the string as a selector, so respondsToSelector:eventSelector
will cause a crash because the selector is actually invalid.
© Stack Overflow or respective owner