The block entity form is as follows: ^ (incoming parameter column) {behavior body};
The block entity starts with "^", followed by a parameter column wrapped in parentheses (such as int a, int b, int c), the body of the action is wrapped in braces, and the exclusive name is called block literal. The behavior subject can return the value by returning, and the type will be automatically identified by compiler. If there are no parameter columns to write: ^ (void).
PS: a block is somewhat similar to a function pointer, but actually represents an underlying data structure, similar to an object, with compilers to create and manage. One advantage of block is that it can use variables of its own extraterritorial scope, for example, a block can read the variable value of its parent scope, which is copied into the data structure of the block heap. When a block is added to the dispatch queue, these values are usually read-only. The declaration of the block is similar to the function pointer, except that the * is changed to ^, and we can pass the parameter to block or receive the value it returns.
Block Access variables
1, can read and block pointer the same scope variable value:
Let's take a look at some interesting code:
{
int OutA = 8;
Int (^myptr) (int) = ^ (int a) {return OutA + A;}; The value of the same type of outa can be read in block
OutA = 5; Change the value of Outa before calling Myptr
int result = MYPTR (3); The value of result is still 11, not 8
NSLog (@ "result=%d", result);
}
Why is the value of result still 11? Instead of 8? In fact, myptr a copy of the value of the outa used in its subject, copy the value of Outa. So, after Outa even replaced with a new value, there is no effect on the value of copy in Myptr.
It is important to note that the copy value here is the value of the variable, if it is a memory location (address), in other words, the variable is a pointer,
Its value can be changed in the block. Here's an example:
{
Nsmutablearray *mutablearray = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil];
int result = ^ (int a) {[Mutablearray removelastobject]; return a*a;} (5);
NSLog (@ "Test array:%@", Mutablearray);
}
The value of the original Mutablearray is {@ "one", @ "one", @"Three"}, after the block is changed Mutablearray, it becomes {@ "one", @ "" "}.
2. Direct access to variables of type static
{
static int outA = 8;
Int (^myptr) (int) = ^ (int a) {OutA = 5; return OutA + A;};
int result = MYPTR (3); The value of result is 8 because Outa is a variable of type static
NSLog (@ "result=%d", result);
}
3. Variables of Block variable type
If you add the modifier word "__block" before a variable (notice that there are two underscores in front of the block), this variable is called block variable. The value of this variable can be arbitrarily modified within the block, as in the following code:
{
__block int num = 5;
Int (^myptr) (int) = ^ (int a) {return num++;};
Int (^MYPTR2) (int) = ^ (int a) {return num++;};
int result = MYPTR (0); Result has a value of 5,num of 6
result = MYPTR2 (0); Result has a value of 6,num of 7
NSLog (@ "result=%d", result);
}
Some understanding of IOS block