I have a C++ helper class that I use with objective-C. I would like to pass the c++ class a block from a view controller (a callback) so that when it is executed I am on the main thread and can update the UI. I currently have a similar system working with function pointers and performSelector
when the function is called. I would like an example of how to set up the c++ variable and how to pass an objective-C block to it and call it from the c++ class.
If this is not possible, can you think of another/better solution?
So is it just that you're not entirely familiar with the block syntax? If so, here's a quick example that should hopefully make sense if you're already familiar with function pointers (the syntax is more or less the same, but with the use of an
^
for declaring one [creating a closure is, of course, different]).You probably want to set up a typedef for the block types just to save yourself repeating the same thing over and over, but I included examples for both using a typedef and just putting the block type itself in the parameters.
See Using a Block as a Function Argument. When declaring your method, use a syntax similar to the following to declare a block argument:
A block call looks like a function call, so an implementation of
theFunction
above which simply calls the block and returns its result would look like this:This syntax will work for C, C++, and Objective-C.