Block is used to save a piece of code.
Block ID: ^Blick is similar to a function. 1. It can save code segments. 2. It has returned values. 3. It has parameters. 4. It has the same calling method.
Block definition:
Return Value Type (^ block name) (parameter type list) = ^ (parameter list) {code segment };
Block call:
Block name (parameter list );
INT (^ sumblock) (INT, INT) = ^ (int A, int B ){
Return A + B;
};Define block variables:
INT (^ sumblock) (INT, INT)Use block to encapsulate the Code:
^ (Int A, int B)
{
Return A + B;
};Block access to external variables:
1. The block can access external variables.
2. By default, external local variables cannot be modified inside the block.
3. Add the _ block keyword to the local variable (the first two underscores), and the local variable can be modified inside the block.Use typedef to define the block type: typedef int (^ myblock) (INT, INT); myblock B1, B2; myblock sumblock = ^ (int A, int B) {return a + B ;}; similar to the pointer to a function: int sum (int A, int B) {return a + B ;}function pointer: int (* P) (INT, INT) = sum; P (10, 12 );
Objective-C: 09_block