Block, Array advanced
Block is essentially an anonymous function (a function without a name)
Block syntax is similar to function pointers
Retrospective function
Function: C language, a code snippet that implements a class of functionality.
The complete function consists of two parts: function declaration, function definition
function declaration, which is the function prototype. For example: int sum (int x,int y), a function with two integer parameters and an integral type return value.
function definition, which is the function implementation. For example: int sum (int x,int y) {
Retrun x + y;
}
Review function pointers
function pointer (variable): A pointer variable that holds the address of a function (the name of a functor).
Int (*p) (int x,int y) = sum;
function pointer type: INT (*) (int x,int y) is a pointer to two integer parameters, an integer that returns a value function.
function pointer variable: The value of the P function pointer: sum
Block
anonymous function: A function without a name. such as int (int x, int y)
Because block is an anonymous function, the implementation of the function that the block variable holds can be directly adjusted by the block variable. function
How does a function with no name be tuned? (If you have a name, you can adjust it directly, and
Can be adjusted by a function pointer)
int(^BLOCK1) (int,int) = ^int(intXinty) {returnX +y; }; //type int (^) (int,int)//variable name Block1//Initial value//^int (int x,int y)// {//return x + y;// }typedefint(^blocktype) (int,int); Blocktype Block2= ^int(intXinty) { returnX *y; }; //the characteristic of assigning a value to a block variable: the definition of the function (implemented), minus the name of the letter
The return value type in the block function definition can be omitted and will usually be omitted
1 int(^AA) () = ^(){2 return Ten;3 };4 //You can omit the function definition () when the block has no parameters.5 //Int (^AA) () = ^{6 //return ten;7 // };8NSLog (@"%d", AA ());9 void(^hello) () = ^{TenNSLog (@"Hello World"); One }; AHello ();//block with no return value without parameters
An external variable can be used directly in the block body, but you cannot modify the variable to add __block before the variable.