views:

156

answers:

2

Hello, I am new to Objective C and I am trying to implement an async library which works with callbacks. I need to figure out a way to pass callback methods as args to my async methods so that the callback can be invoked when the task is finished.

What is the best way to achieve this in Objective C? In Python, for example I could easily pass a function, but in Objective C it seems selectors are the way to go(?).

Can anyone point me to an example from where I can get some ideas?

Thanks in advance.

+4  A: 

Selectors are ONE way to go... the alternative is to create a protocol and to require invokers of your API to provide a "delegate" object that implements the protocol. Then you can invoke the required selectors of that protocol on the delegate object that you have been given. Each has its advantages and disadvantages.

Example using selectors: NSThread:detachNewThreadSelector:toTarget:withObject

Example using delegates:NSXMLParser:setDelegate + NSXMLParser:parse

Michael Aaron Safyan
A: 

I think I have found the answer to my question in this question.

This example code at Apple's developer site was very useful.

Baishampayan Ghose