I'm using respondsToSelector:
and most of the time it works fine. But there is one case in which I get wrong result:
UIButton* button = [[UIButton alloc] init];
if([button respondsToSelector:NSSelectorFromString(@"setTitle:")]) // returns YES
{
// try to call "setTitle:"
}
respondsToSelector:
returns YES but there is no setTitle:
selector in UIButton class. There is setTitle:forState:
selector but this is definitely not the same.
So why does respondsToSelector:
return YES?
Responds to selector doesn't just check the public interface, it'll take any method it can find. I don't recall if the early API for UIButton
ever exposed the title directly, but internally it's likely called as the state changes.
Try to only use respondsToSelector:
for API that you actually need to verify exists, and note that there is often private API which is later made public and that this can also cause interesting situations...