Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6904083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:00:23+00:00 2026-05-27T08:00:23+00:00

I want to use a block as a callback function. I store my block

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:00:24+00:00Added an answer on May 27, 2026 at 8:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a category class that I want to Use that acts on a
Why you would want to use a switch block over a series of if
I want to use to_sentence method on the block of code to return the
I want to use RSpec mocks to provide canned input to a block. Ruby:
I have a transaction log file in CSV format that I want use to
I want to use the same {% block %} twice in the same django
I want to use jQuery to animate a simple vertical menu that can be
In a specific template i want to use a block, so it's contents can
I want to be able to do the callback of the .load() function when
I have 2 CAS servers on my primary site.I want to use Allow/Block/Quarantine feature

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.