Function pointers in Objective-C
- by Stefan Klumpp
I have the following scenario:
Class_A
- method_U
- method_V
- method_X
- method_Y
Class_B
- method_M
- method_N
HttpClass
- startRequest
- didReceiveResponse // is a callback
Now I want to realize these three flows (actually there are many more, but these are enough to demonstrate my question):
Class_A :: method_X -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_A :: method_Y:result
and:
Class_A :: method_U -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_A :: method_V:result
and the last one:
Class_B :: method_M -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_B :: method_N:result
Please note, that the methods in Class_A and Class_B have different names and functionality, they just make us of the same HttpClass.
My solution now would be to pass a C function pointer to startRequest, store it in the HttpClass and when didReceiveResponse gets called I invoke the function pointer and pass the result (which will always be a JSON Dictionary).
Now I'm wondering if there can be any problems using plain C or if there are better solutions doing it in a more Objective-C way. Any ideas?