I have searched around for a while, but surprised that no one ever popped this question, so here I go:
I have a Objective-C method that takes an id argument. Now I want to use respondsToSelector:
to determine if it is valid for following actions, if it is not not I have a backup plan for it. (Which is not ASSERT(0)
:P)
So, when someone tries to mess up with me and pass an (id)someC++Object
, I've got a big crash, of course.
My question is simply: Can I make a C++ object fail a respondsToSelector:
safely so that I can use my backup plan (punching the coder in the face for one)?
In short, you can't.
At least, not reliably and not in any sane way. Nor, frankly, should bother; that level of defensive programming is utterly futile in the face of a C based language.
At the least, you would need to:
In general, any code that makes heavy use of isKindOfClass:
and respondsToSelector:
(outside of the delegation pattern) is quite atypical to the norms of Objective-C design patterns.
Or, to put it another way, you have a static type checking compiler that is quite good at checking lots of validity parameters at compile time. Use it.