Simple use of IOS block and __block and static modifier variables

Source: Internet
Author: User

Simple code Summary, the shortcomings of a lot of advice.

//Simple to use-(void) blockone{intNumber= at; int(^blockone) (int) = ^(intnum2) {                returnnumber*num2;            }; NSLog (@"block%d for output", Blockone (2)); }-(void) blocktwo{int(^block) (int,int) = ^(intStra,intstr2) {                returnStra +str2;            }; NSLog (@"This is the second one.%d", Block (2,3)); }-(void) blockthree{//this represents the square root of the input (int A is the parameter, return a*a; is the subject of the action). Remember to add ";" because it is a narrative, and the whole {} is followed by a ";" because block is an object entity.//the 3 inside the parentheses will be treated as an input value and then the block output 3*3=9 call the reblock variable.            intReblock = ^ (intA) {returnA*a;} (3); NSLog (@"reblock%d", reblock); //the above simple method callback value (^ name) (parameter column);        int(^square) (int); //there is a square block Pointer The block it points to has an int input and outputSquare= ^(intA) {returnA*a;};//Put the entity just now to square//UseNSLog (@"Square%d of output square", Square (2)); }

Changes in the value of a variable in a block inside and outside the function

-(void) blockpointer{#defineWhether the change of the mark variable can change its value//1 unmodified Block outside change Outa does not change the final result       intOutA =8;
int(^MYPTR) (int) = ^(inta) {returnOutA +A; }; OutA=Ten;
//go to a block in the same scope value

intresult = Myptr (3);
NSLog (@"block outside the change outa value of 10 will be 13 after%d", result);///This value will be copied to block for the first time in the block, and the next time you use the copy value directly, the value of the defense is not affected by the value of the variable; if it is a pointer pointer, its value can be changed in the Block //2 __block Modified after modification, can change in the inside change, can change//external Changes__blockintOutB =8;
int(^MYPTRB) (int) = ^(intb) {
returnOutB +b; }; OutB=Ten;
//go to a block in the same scope value intRESULTB = MYPTRB (3); NSLog (@"block outside the change OUTB value of 10 will be 13 after%d", RESULTB); //Block Internal Change__blockintOUTC =8; int(^MYPTRC) (int) = ^(intc) {OUTC=Ten; returnOUTC +C; }; NSLog (@"block Internal variable OUTC value of 10 will be 13 after%d", MYPTRC (3)); }

#defineChange in value when Mark static modifies a variable//modification with Staic is a change, because OUTC is a static variable that responds directly to its value .-(void) blockstaticchange{//2 __block Modified after modification, can change in the inside change, can change//external Changes Static intOutB =8; int(^MYPTRB) (int) = ^(intb) {returnOutB +b; }; OutB=Ten; //go to a block in the same scope value intRESULTB = MYPTRB (3); NSLog (@"static external Change outb value of 10 will be 13 after%d", RESULTB); //Block Internal Change Static intOUTC =8; int(^MYPTRC) (int) = ^(intc) {OUTC=Ten; returnOUTC +C; }; NSLog (@"static internal variable OUTC value of 10 will be 13 after%d", MYPTRC (3)); }

Simple use of IOS block and __block and static modifier variables

Related Article

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.