In Objective-C/C, can you write a function that combines 2 blocks?

前端 未结 5 1275
北荒
北荒 2020-12-12 00:30

I often find myself creating a \"wrapper\" block which just serves to execute a number of other blocks, usually with the same type signature.

Say I have 2 blocks wit

5条回答
  •  独厮守ぢ
    2020-12-12 01:19

    Is this what you are looking for?

    MyBlockT CombineBlocks(MyBlockT block1, MyBlockT block2)
    {
        return [^(NSString *string, id object) {
            block1(string, object);
            block2(string, object);
        } copy];
    }
    

    The function creates a new block that calls the two given blocks sequentially.

提交回复
热议问题