Executing Blocks From NSArray?

前端 未结 3 1538
囚心锁ツ
囚心锁ツ 2021-01-31 08:35

I was just thinking, as you can treat Blocks like objects if I create two of them and then add them to an NSArray is there a way to execute them from the array?

         


        
相关标签:
3条回答
  • 2021-01-31 09:00

    Of course you can.

    int (^x)(void) = [array objectAtIndex:0];
    printf("%d\n", x()); // prints 101.
    
    0 讨论(0)
  • 2021-01-31 09:01

    Sure, you just invoke it with () like any other block, but you need to typecast the value you retrieve from NSArray. Here's an example (with an added typedef, because otherwise my head hurts):

    typedef int (^IntBlock)(void);
    IntBlock Block_001 = ^{ return 101; };
    IntBlock Block_002 = ^{ return 202; };
    NSArray *array = [NSArray arrayWithObjects:Block_001, Block_002, nil];
    int x = ((IntBlock)[array objectAtIndex:0]) (); // now x == 101
    
    0 讨论(0)
  • 2021-01-31 09:18

    @KennyTM and @David are correct, but your code is potentially wrong. Here's why:

    When creating an NSArray with objects, it will retain the objects put into it. In the case of blocks, it's using the Block_retain function. This means that the array has retained the blocks that you created, but that live on the stack (blocks are one of the very rare examples of Objective-C objects that can be created on the stack without delving into absurd tricks). That means that as soon as this method exits, your array now points to garbage, because the blocks it was pointing to no longer exist. To do this properly, you should do:

    int (^Block_001)(void) = [^{ return 101; } copy];
    int (^Block_002)(void) = [^{ return 202; } copy];
    NSArray *array = [NSArray arrayWithObjects:Block_001, Block_002, nil];
    
    [Block_001 release];
    [Block_002 release];
    

    By invoking copy on the block, you're explicitly moving the block off of the stack and onto the heap, where it can safely remain after the method/function exits. Then after you've added the blocks to the array, you have to balance your copy (because of the NARC rule) with a subsequent call to release. Make sense?

    0 讨论(0)
提交回复
热议问题