[IOS development] overturn the incorrect conclusion-"Block can capture the state of its declaration in the same scope ."

Source: Internet
Author: User

Looking at block recently, I feel amazing.

In particular, block can capture the automatic variable value feature, which is a big killer.

So when is the automatic variable captured?

Major technical blogs and technical books share the same caliber: Block can capture the state of its declaration in the same scope.

Is that true?

Try it with me.


Code:

-(Void) viewDidLoad

{

Int val = 0;

Void (^ blk) (NSInteger); // declare block type variables

Val = 1; // assign a value before definition.

Blk = ^ (NSInteger ){

NSLog (@ "intercepted val: % d", val );

Return;

};

Val = 2; // After definition, assign a value before calling.

Blk (val );


}

Pre-declaration val = 0;

After the declaration, the pre-defined val = 1;

Pre-call val = 2 after definition;

What are the printed results ??


18:30:14. 182 Test [2594: 70b]InterceptedVal: 1



So we come to the conclusion:

Block can capture itsDefinitionStatus of the same scope. Instead of the state of the same domain where the declaration is located.

See Apple's official documentation.

Https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Blocks/Articles/bxOverview.html#//apple_ref/doc/uid/TP40007502-CH3-SW3

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12022511H-0.png "title =" QQ20131227-1@2x.png "alt =" 183223845.png"/>

  • Can capture state from the lexical scope within which it is defined

Block) can capture itsDefinitionStatus of the same scope.

Shen Zheng Zhao Xue ......


Why is the definition captured in the same scope on the Internet?

Shunteng found that someone on the Internet translated this apple official document about block into Chinese...

The translation is quite good, that is, the translation is wrong ...... Translate the definition into a declaration or be careful, or the declaration and definition concepts are not clearly understood)

The block-related materials have evolved from this incorrect translation document...

Sadly, no one has yet named it...

Baidu's conclusions about when Block captures a specific scope are all false ......

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1202254Q2-1.png "title =" QQ20131227-2@2x.png "alt =" 181091693.png"/>

Ah !!


Correct conclusion:

Block should capture itDefinitionStatus of the same scope. Instead of the state of the same domain where the declaration is located.

Do not blindly trust the information on the Internet. First, try it on your own, and second, be cautious with translation errors.

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.