Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphic property blocks in Obj C as callback functions

I want to use a block as a callback function. I store my block as a property in my class that does the calling back:

@interface MyParameter
{
    float myValue;
    void (^callback)(float);
}
@property(copy) void (^callback)(float);
@end

@implementation MyParameter
@synthesize callback;

- (void) valueChanged
{
    callback(myValue);
}
@end

Then I set the callback:

MyParameter * param = [[MyParameter alloc] init];
[param setCallback:^(float value) { [self doSomethingWithValue:value]; }];

So far so simple. In reality, MyParameteris a generic abstraction which holds details on a runtime-determined number of different types of parameter. I can have continuous (float) values, discrete values (int) and booleans. So next, I tried this:

@interface MyParameter
{
    float floatVal;
    int intVal;
    void (^contCallback)(float);
    void (^discCallback)(int); // And a boolean one too but let's keep this short
    ParamType type; // enum of types ie enum ParamType { Continuous, Discrete, Boolean };
}
@property(copy) void (^contCallback)(float);
@property(copy) void (^discCallback)int);
@end

@implementation MyParameter
@synthesize contCallback;
@synthesize discCallback;

- (void) valueChanged
{   
    switch (type) {
    case Continuous: 
        contCallback(floatVal);
        break;
    case Discrete:
        discCallback(intVal);
        break; 
    // Leave default out for brevity
    }
}
@end

Which is getting uglier. I want to keep just one ^callback as an ivar/property. I then want to set it like this:

MyParameter *contParam = [[MyParameter alloc] init];
[contParam setCallback:(^float value) { [self doSomethingContinuous:value]; }];

MyParameter *discreteParam = [[MyParameter alloc] init];
[discreteParam setCallback:(^int value) { [self doSomethingDiscrete:value]; }];

Inside MyParameter my valueChanged method would retain the switch/case from above, to decide what to pass in to the callback.

Is this possible?

like image 929
Tim Avatar asked Mar 19 '26 00:03

Tim


1 Answers

Pass your parameter as an NSNumber. You can recover its original type using -objCType which will return a string indicating a type encoding. This way you don't have to polymorph your function call.

Alternately, if NSNumber turns out to be too inflexible, just create your own Argument data class that encodes whatever information you need. If necessary, you can also create a Result data class for the return type.

like image 175
Rob Napier Avatar answered Mar 21 '26 00:03

Rob Napier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!