Some minor notes about block access to external variables
The Block type is a C-level syntax and operating mechanism. It is similar to the Standard C function, except that it contains executable code and variables bound to the heap and stack memory. Therefore, the Block object contains a set of status data, which is used to affect the behavior during program execution.
You can use Block to write some function statements that can be passed to the API, store them selectively, and use multiple threads. As a callback, Block is particularly useful because it contains both the Code during the callback and the data required during execution.
Since both Objective-C and C ++ are derived from C, the block is designed to be compatible with these three languages at the same time.
Today, we mainly talk about some things about block access to external variables!
1) You can access variables outside the block within the block.
External variables can be used inside the block.
Note that this is a new memory space variable.
You can also define a variable (local variable) with the same name as the external variable of the block. At this time, the local variable will temporarily block the scope of the external variable.
2) the block "external variables" cannot be modified inside the block"
Non-local variables are copied and stored in the block as the const variable, that is, they are read-only in the block. If you try to assign values to the m variable within the block, a compiler error is thrown.
Non-local variables in the freeze are a constant value, which is also a default security behavior-because it can prevent the code in the block from making unexpected changes to non-local variables.
Add local variables__block
Keyword, the local variable can be modified within the block. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20160408/20160408091645204.png" alt = "\">
Note:
1. Static and global variables are added and not added.__block
Will Directly reference the variable address. This means that the value of the variable can be modified. Add__block
Keyword.
2. Constant variables (NSString *a=@"hello";
a
Is a variable,@“hello”
Is a constant .)
Do not add__block
Type. block directly obtains the constant value (shallow copy ).
Add__block
Type. block references the variable address. (For example, a variable, a = @ "abc". You can arbitrarily modify the content pointed to by .)
If you do not add__block
If you modify the variable directly within the block, an error will be reported during compilation. The internal change volume of the block is read-only.