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"/>
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.