Access to an array of C blocks inside a block

I have a C-array of blocks defined as follows:

void (^block1)() = ^void() {
  doSomething1();
  doSomething2();
};

void (^block2)() = ^void() {
  doSomething3();
  doSomething4();
};

// etc.
typedef void (^myBlockType)();
myBlockType arrayBlock[2];
arrayBlock[0] = block1;
arrayBlock[1] = block2;

but then I try to access this C array from inside the block, something like

void (^adjust)(int value) = ^void(int value) {
    myBlockType myBlock = arrayBlock[value];
};

and Xcode whines with

Cannot reference an ad with an array type inside a block

If it was an array of things like float, integers, etc., there is this solution , but how to do it for an array of C blocks?

Can i do

    myBlockType array = calloc(2, sizeof(myBlockType));

? Is this the right approach? In this case, how to initialize the values?

+4
source share
1 answer

OK, some pointers after the comments follow, but you need to find out and understand what is happening here.

Can i do

myBlockType array = calloc(2, sizeof(myBlockType));
  • C . C T x[n] x T * - . x[n] , *(x + n), (* +). , , . "" .
  • , .
  • , , . , , , .
  • calloc - , , array .
  • , ARC ( ) , . , array . , __strong, "" , , NSArray.
  • , calloc ( malloc .), free, .

( ) .

0

Source: https://habr.com/ru/post/1682416/


All Articles