Blocks are Apple’s implementation of closures for C, which are also available for Objective-C and C++.
Blocks are an Apple extension to C, often used in conjunction with Objective-C. When used from Objective-C, blocks also function as full Objective-C objects.
They are more traditionally known as closures. As closures, they can capture variables from the surrounding scope, be passed to functions, and be stored in variables. The syntax of a Block type is nearly identical to a function pointer, with *
being replaced by ^
.
An example of block definition is the following one:
int (^minusOne)(int);
minusOne = ^(int anInt) {
return anInt - 1;
};
For more examples regarding how to declare various types of blocks, see How Do I Declare A Block In Objective-C?, and for more in-depth information, see Blocks Programming Topics.