Objective-c-code blocks block

Source: Internet
Author: User

Block blocks of code
/* What the block wants to know 1> how to define the block variable int (^sumblock) (int, int), void (^myblock) (), 2> How to use block encapsulation code ^ (int a, int b) { return a-B; }; ^ () {NSLog (@ "----------");}; ^ {NSLog (@ "----------");}; 3> block Access outside variable * block inside can access the outside of the variable * By default, the inside block cannot modify the outside local variable * to the local variable plus the __block keyword, this local variable can be modified within the block 4> Use typedef to define the block type typedef int (^myblock) (int, int); You can then use this type of Myblock to define the block variable Myblock block; Myblock B1, B2; B1 = ^ (int A, int.) {return a-B;}; Myblock B3 = ^ (int A, int.) {return a-B;}; */typedef int(*sump) (int,int);typedef int(^myblock) (int,int);intSumintAintb) {returnA + b;}intMain () {//Int (*p) (int, int) = SUM;    //Int (*P2) (int, int) = SUM;    //Sump p = sum;    //sump p2 = sum;    / * INT (^sumblock) (int, int);    Sumblock = ^ (int a, int b) {return a + B;    };    Int (^minusblock) (int, int) = ^ (int A, int.) {return a-B; };*/Myblock Sumblock; Sumblock = ^ (intAintb) {returnA + b;    }; Myblock Minusblock = ^ (intAintb) {returnA-B;    }; Myblock Multiplyblock = ^ (intAintb) {returnA * b; };NSLog(@"%d-%d-%d", Multiplyblock (2,4), Sumblock (Ten,9), Minusblock (Ten,8));return 0;}voidTest3 () {intA =Ten; __blockintb = -;void(^block)    (); block = ^{//block internal access to external variables        //nslog (@ "a =%d", a);        //By default, the outer local variables cannot be modified inside the block        //a =;        //To add the __block keyword to the local variable, this local variable can be modified within the blockb = -;    }; Block ();}//block with return value, tangible parametervoidTest2 () {/*//pointer function pointer Int (*P) (int, int) = SUM;     int d = P (10, 12);     NSLog (@ "%d", d); */    int(^sumblock) (int,int) = ^(intAintb) {returnA + b; };intc = Sumblock (Ten, One);//Use a block to output n horizontal lines    void(^lineblock) (int) =  ^(intN) { for(inti =0; i<n; i++) {NSLog(@"----------------");    }    }; Lineblock (5);}//No return value, block with no formal parametersvoidTest () {//block to save a piece of code    //block flag: ^    /* block is similar to the function: 1. You can save code 2. There is a return value of 3. Physical Parameters 4. Call the same way * /    //define block variables    / * void (^myblock) () = ^ () {NSLog (@ "----------------");    NSLog (@ "----------------"); };*/    //If the block has no formal parameters, you can omit the following ()    void(^myblock) () = ^{NSLog(@"----------------");NSLog(@"----------------"); };//Use block variable to invoke code inside blockMyblock (); Myblock ();}

Objective-c-code blocks block

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.