Storing Blocks in an Array

前端 未结 2 1863
南笙
南笙 2020-12-04 07:37

In Objective-C, I know that blocks are considered objects, so I was wondering if it was possible to store them in an array. This begs the question, are blocks first class o

2条回答
  •  渐次进展
    2020-12-04 08:39

    Yes, blocks are indeed objects, and you can put them in arrays:

    NSMutableArray *arr = [NSMutableArray new];
    [arr addObject:^(){NSLog(@"my block");}];
    void (^ myblock)() = [arr objectAtIndex:0];
    myblock();
    

    this will put the "my block" in the console.

提交回复
热议问题