Como alternativa un poco más completa al ejemplo de KKK4SO:
#import <Cocoa/Cocoa.h>
// typedef for the callback type
typedef int (*callbackType)(int x, int y);
@interface Foobar : NSObject
// without using the typedef
- (void) callFunction:(int (*)(int x, int y))callback;
// with the typedef
- (void) callFunction2:(callbackType)callback;
@end
@implementation Foobar
- (void) callFunction:(int (*)(int x, int y))callback {
int ret = callback(5, 10);
NSLog(@"Returned: %d", ret);
}
// same code for both, really
- (void) callFunction2:(callbackType)callback {
int ret = callback(5, 10);
NSLog(@"Returned: %d", ret);
}
@end
static int someFunction(int x, int y) {
NSLog(@"Called: %d, %d", x, y);
return x * y;
}
int main (int argc, char const *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Foobar *baz = [[Foobar alloc] init];
[baz callFunction:someFunction];
[baz callFunction2:someFunction];
[baz release];
[pool drain];
return 0;
}
Básicamente, es el mismo que cualquier otra cosa, excepto que sin el typedef, no se especifica el nombre de la devolución de llamada cuando se especifica el tipo del parámetro (el parámetro callback
en el método callFunction:
). Así que ese detalle podría haberlo estado haciendo tropezar, pero es bastante simple.
¿Podría mostrar lo que está utilizando en este momento? – nil