Blocks are typically used to represent and simplify a small piece of code, which is particularly well-suited for creating some program fragments that are executed synchronously, encapsulating small jobs, or calling back paging (callback) when a job is completed.
The block entity form is as follows:
^ (Incoming parameter column ) { behavior body };
1:block can access local variables, but cannot be modified.
int multiplier = 7;
Int (^myblock) (int) = ^ (int num) {
Multiplier ++;//Compile Error
return num * multiplier;
};
Add keywords If you want to modify: __block
__block int multiplier = 7;
Int (^myblock) (int) = ^ (int num) {
Multiplier ++;//Compile Error
return num * multiplier;
};
2: As a function parameter, blocks replaces the callback function or delegate in some sense. When a function is called, it is assumed that an event is triggered, and then the contents of the blocks run. This facilitates the integration of code and reading, you do not need to go everywhere to implement the delegation method.
3:block pointer is defined in this way:
Callback Value (^ name ) ( parameter column );
4:///5: You can modify the value of the Outa directly inside the block,
static int outA = 8;
__block int OutA = 8;
Int (^myptr) (int) = ^ (int a) {OutA = 5; return OutA + A;};
int RESULT3 = MYPTR (3); The value of result is 8 because Outa is a variable of type static
NSLog (@ "result=%d", RESULT3);
IOS block use