[IOS] What does _ block in Obnjective-C mean?
The _ block Mark tells the compiler that special processing is required for this variable in the block.
Generally, the variable value used in the block is copied, so the modification to the variable itself does not affect the real value of the variable. When _ block is used, it indicates that the modification in the block is also effective outside the block.
For details, see https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html, Which is Apple's official explanation of block.
Next, let's look at an example:
extern NSInteger CounterGlobal;static NSInteger CounterStatic;{ NSInteger localCounter = 42; __block char localCharacter; void (^aBlock)(void) = ^(void) { ++CounterGlobal; ++CounterStatic; CounterGlobal = localCounter; // localCounter fixed at block creation localCharacter = 'a'; // sets localCharacter in enclosing scope }; ++localCounter; // unseen by the block localCharacter = 'b'; aBlock(); // execute the block // localCharacter now 'a'}
In the above Code, both localConter and localCharacter are modified in the block, but in the block, only the modification of localCharacter is valid because the _ block Mark plays a role. Modifications to localCharacter in the block are also visible outside the block.