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

  • Home
  • SEARCH
  • 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 9137397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:05:55+00:00 2026-06-17T09:05:55+00:00

I have a variable with a block accepting some arguments. Exact number of arguments

  • 0

I have a variable with a block accepting some arguments. Exact number of arguments and their types can vary. For example it can be a block

void(^testBlock1)(int) = ^(int i){}

or a block

void(^testBlock2)(NSString *,BOOL,int,float) = ^(NSString *str,BOOL b,int i,float f){}

Argument types are limited to {id, BOOL, char, int, unsigned int, float}.

I know the current count of arguments and their types. I need to implement a method that can execute block with given arguments:

-(void)runBlock:(id)block withArguments:(va_list)arguments 
          types:(const char *)types count:(NSUInteger)count;

I have one working naive solution, but it is quite ugly, supports only types of no more than 4 bytes size and relies on alignment. So I’m looking for something better.
My solution is something like:

#define MAX_ARGS_COUNT 5
-(void)runBlock:(id)block withArguments:(va_list)arguments 
          types:(const char *)types count:(NSUInteger)count{

    // We will store arguments in this array.
    void * args_table[MAX_ARGS_COUNT];

    // Filling array with arguments
    for (int i=0; i<count; ++i) {
        switch (types[i]) {
            case '@':
            case 'c':
            case 'i':
            case 'I':
                args_table[i] = (void *)(va_arg(arguments, int));
                break;
            case 'f':
                *((float *)(args_table+i)) = (float)(va_arg(arguments, double));
                break;
            default:
                @throw [NSException exceptionWithName:@"runBlock" reason:[NSString stringWithFormat:@"unsupported type %c",types[i]] userInfo:nil];
                break;
        }
    }

    // Now we need to call our block with appropriate count of arguments

#define ARG(N) args_table[N]

#define BLOCK_ARG1 void(^)(void *)
#define BLOCK_ARG2 void(^)(void *,void *)
#define BLOCK_ARG3 void(^)(void *,void *,void *)
#define BLOCK_ARG4 void(^)(void *,void *,void *,void *)
#define BLOCK_ARG5 void(^)(void *,void *,void *,void *,void *)
#define BLOCK_ARG(N) BLOCK_ARG##N

    switch (count) {
        case 1:
            ((BLOCK_ARG(1))block)(ARG(0));
            break;
        case 2:
            ((BLOCK_ARG(2))block)(ARG(0),ARG(1));
            break;
        case 3:
            ((BLOCK_ARG(3))block)(ARG(0),ARG(1),ARG(2));
            break;
        case 4:
            ((BLOCK_ARG(4))block)(ARG(0),ARG(1),ARG(2),ARG(3));
            break;
        case 5:
            ((BLOCK_ARG(5))block)(ARG(0),ARG(1),ARG(2),ARG(3),ARG(4));
            break;
        default:
            break;
    }
}
  • 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-06-17T09:05:55+00:00Added an answer on June 17, 2026 at 9:05 am

    Well you are running up against the classic lack-of-metadata and ABI problem in C here. Based on Mike Ash’s Awesome Article about MABlockClosure, I think you could examine the block’s underlying struct and assume that the va_list matches what the block expects. You can cast the block to struct Block_layout, then block->descriptor will give you the struct BlockDescriptor. Then you have the @encode string that represents the block’s arguments and types (@encode being a whole other can of worms).

    So once you have the list of arguments and their types, you can dig into the block_layout, grab invoke, then treat it as a function pointer where the first parameter is the block that provides the context. Mike Ash also has some information on Trampolining Blocks that might work if you don’t care about any of the type information, but just want to invoke the block.

    Let me add a big fat “There be dragons here” warning. This is all very touchy, varies based on the ABI, and relies on obscure and/or undocumented features.

    It also seems like you could just call the block directly wherever it was needed, possibly using an NSArray as the only parameter and id as the return type. Then you don’t have to worry about any “clever” hacks backfiring on you.

    edit: You might be able to use NSMethodSignature’s signatureWithObjCTypes:, passing in the block’s signature. Then you can call NSInvocation’s invocationWithMethodSignature:, but you’ll have to call the private invokeWithIMP: method to actually trigger it because you don’t have a selector. You’ll set the target to the block, then invokeWithIMP, passing the Block struct’s invoke pointer. See Generic Block Proxying

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

Sidebar

Related Questions

How I can have variable number of parameters in my function in C++. Analog
I have function, where I add block variable blockIfLoadingImageFromServer to array: - (UIImage *)getImageWithblockIfLoadingImageFromServer:(void
I have a static block variable inside a class. How can I declare a
In Javascript, I have a variable that is set to a block of text
I have variable WCHAR sDisplayName[1024]; How can I check if sDisplayName contains the string
I have a variable size block of text, which I would like centered in
I have a a block for enumeration and some properties in an .h file
I have a variable number of divs acting as columns, and I would like
Let's assume I have the following typedef: typedef void (^myBlock)(id); And I have some
The idea is to have a variable number of channels in a slice, push

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.