Syntax to define a Block that takes a Block and returns a Block in Objective-C

后端 未结 2 1220
野性不改
野性不改 2020-12-15 10:32

I find in Apple\'s document Working with Blocks that the syntax to define a block that returns the result of multiplying two values:

double (^multiplyTwoValu         


        
2条回答
  •  无人及你
    2020-12-15 11:11

    Obj-C block syntax is pretty hard to read, this can be simplified a bit with the use of typedefs.

    //setup
    typedef void (^ReturnedBlock)(void);
    ReturnedBlock retBlock = ^void(void){};
    
    typedef void (^ParamBlock)(void);
    ParamBlock paramBlock = ^void(void){};
    
    //the thing you want to do
    ReturnedBlock (^someBlock)(ParamBlock) = ^ReturnedBlock(ParamBlock param){
    
        return retBlock;
    };
    
    //perform the block
    ReturnedBlock r = someBlock(paramBlock);
    

提交回复
热议问题