I am not familiar with class_addMethod, but maybe this can help you clarify:
Remember that in Objective-C you are not calling a method, but you are really sending a message. So it's safe to do: [anyObject anyMethodName] on any instance of the object. This object may or may not reply to a message.
You can check whether or not the object will be using [anyObject responds to SoSelector: @selector (@ "anyMethodName")], and if it is YES, then continue and call [anyObject anyMethodName]. I cannot fully understand your description of the problem, but it looks like you have a heterogeneous container full of objects that may or may not answer the call. Doing this "responsesToSelector:" checking every object in a container is an absolutely normal thing in Objective-C and sounds like a good design
If each object returns some other data type, you can process it using the id type. That is, id returnData = [anyObject anyMethodName]; Then you can use introspection in returnData or you can handle things differently depending on what class "anyObject" is checked by [class anyObject];
So that,
if([anyObject class] == MyGreatClass) // recast data to MyGreatClassCoolReturnType
Hope this helps answer the question.
Nektarios
source share