I've been reading up on Objective-C blocks (e.g. in the Apple documentation, a blog post, and one or two or three Stack Overflow answers). I want to pass a C / C++ style callback into an Objective-C method.
Here's my declaration on the C / C++ side
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*CALCULATION_CALLBACK)(int x);
void setCubeCallback(int x, CALCULATION_CALLBACK callback);
#ifdef __cplusplus
}
#endif
and in Objective-C
@interface IOSPluginTest : NSObject
typedef void (^CalculationHandler)(int x);
-(void)cubeThisNumber:(int)number andCallbackOn:(CalculationHandler)callback;
@end
And this is the Objective-C implementation
#import "IOSPluginTest.h"
@implementation IOSPluginTest
-(void)cubeThisNumber:(int)number andCallbackOn:(CalculationHandler)callback {
int result = number * number * number;
if (callback != nil) {
callback(result);
}
}
@end
Things go wrong in the last bit, the C / C++ implementation
void setCubeCallback(int x, CALCULATION_CALLBACK callback) {
[[[IOSPluginTest alloc] init] cubeThisNumber:x andCallbackOn:callback];
}
which fails to compile with the error
Sending 'CALCULATION_CALLBACK' (aka 'void(*)(int)') to parameter of incompatible type 'CalculationHandler' (aka 'void(^)(int)')
Those two type descriptions, void(*)(int)
and void(^)(int)
, look pretty similar to me; what am I missing?