Function pointers in Objective-C

Posted by Stefan Klumpp on Stack Overflow See other posts from Stack Overflow or by Stefan Klumpp
Published on 2010-05-11T13:42:29Z Indexed on 2010/05/11 13:44 UTC
Read the original article Hit count: 266

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?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about function-pointers