[IOS] What does _ block in Obnjective-C mean?

Source: Internet
Author: User

[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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.