Dark Horse programmer-oc language learning experience-block

Source: Internet
Author: User

Dark Horse Programmer-oc Language learning experience-block

-------Java training , Android training ,iOS training ,. Net Training , look forward to communicating with you!  -------

1, the program typically uses typedef to define a block type and then call

typedef void(^XXX) ();

can use mnemonic inlineblock

2,block internal access to external variables;
By default, external local variables cannot be modified inside the block

To add the __block keyword to the local variable, the local variable can be modified inside the block.

The role of 3,block:

Block encapsulation code can be used as function return values and function parameters.

Increased code flexibility and efficiency

Here are the code implementations that you write yourself

#import<Foundation/Foundation.h>//void (^workblock) () block seat function parameter transfertypedefvoid(^Newbolck) ();voidWorkintN) {NSLog (@"get up .... "); NSLog (@"brush your teeth .... "); NSLog (@"wash your face .... "); NSLog (@"go to the company .... "); //Workblock (); //not the same here .Newbolck WorkDay (inta);//declares a method, because the method body is belowNewbolck W=workday (n);//the return value for Workday (n) Here is Newblock objectW ();//Execute code blockNSLog (@"go home .... ");} typedefvoid(^NewType) ();//The return value is block typeNewType Test () {NewType N1=^{NSLog (@"xxxxxxx"); NSLog (@"yyyyyyyy");    }; returnN1;}//A block with two parameters to do the return valuetypedefint(^newtype2) (int,int); newType2 test2 () {return^(intAintb) {            returnA +b; };}//let him return a value of block typeNewbolck WorkDay (inta) {        //Simplify//typedef void (^block) ();//Defining aliasesNewbolck W;//define BOLCK type variable without adding * number .....        Switch(a) { Case 1: w=^() {NSLog (@"Understanding the Project");        };  Case 2: w=^() {NSLog (@"development Projects");        };  Case 3: w=^() {NSLog (@"sold the project");            };  Break; default:             Break; }       //Work (w);    returnW;}intMainintargcConst Char*argv[]) {@autoreleasepool {//for (int i=1; i<=5; i++) {Work (1); // }                        //call return value to block type method//because the test () method returns a value that is a block object, the object is created to acceptNewType n2=test ();                N2 (); NewType2 N3=test2 (); intRESULT=N3 (3,8); NSLog (@"result=%d", result); WorkDay (1); printf ("\ n"); WorkDay (2); printf ("\ n"); WorkDay (3); printf ("\ n"); void(^myblock) (int,int)=^(intAintb) {NSLog (@"a+b=%d", A +b);         }; Myblock (2,8); typedefint(^NEWBOLCK) (int,int); Newbolck B1=^(intXinty) {returnx+y;                }; Newbolck B2=^(intXinty) {                            returnX>y?x:y;                               }; NSLog (@"b1->%d", B1 (2,5)); NSLog (@"b2->%d", B2 (5,9)); }                return 0; }

Dark Horse programmer-oc language learning experience-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.