24th-day notes for IOS (Block Introduction) and 24th-day ios

Source: Internet
Author: User

24th-day notes for IOS (Block Introduction) and 24th-day ios

Knowledge points of IOS Learning (oc language)

I. Introduction of Block

1) concept: block is a data type. It is similar to a function without a name in C language. It can receive parameters or return values that are called like C functions.

Encapsulate a piece of code that can call a block anywhere or as a function parameter, and return a function value.

2) Block instance code

1 // defines a block type MyBlock. A variable of the MyBlock type can only point to the parameter with two int types and the code block that returns the int type 2 typedef int (^ MyBlock) (int, int ); 3 // define a function pointer 4 int (* pMath) (int, int); 5 6 int add (int a, int B) 7 {8 return a + B; 9} 10 11 int sub (int a, int B) 12 {13 return a-B; 14} 15 16 int main (int argc, const char * argv []) {17 @ autoreleasepool {18 pMath = add; // point to function pointer 19 // NSLog (@ "sum: % d", pMath (2, 3); 20 pMath = sub; 21 22 // defines a block. Can point to a parameter with two int values, return the int code block 23 // start with ^ as the code block, followed by () as the parameter, and then {} code block 24 int (^ bloke1) (int, int) = ^ (int a, int B) {25 return a + B; 26}; 27 28 int s = bloke1 (3,5 ); 29 NSLog (@ "s: % d", s); 30 // define a block to point to a code block without a parameter and no return value (No parameter, void can be omitted) 31 void (^ block2) (void) = ^ {32 NSLog (@ "programing is fun! "); 33}; 34 block2 (); 35 int (^ block3) (int, int) = ^ (int a, int B) {36 return a-B; 37 38}; 39 40 // defines a variable of the MyBlock type, and assigns a value to the Code Block 41 MyBlock block4 = ^ (int a, int B) {42 return a * B; 43 }; 44 45 NSLog (@ "% d", block4 (2, 5); 46 47 int c = 10; 48 _ block int d = 1; 49 // The block can access the variable outside the block but cannot be modified. If you need to modify the variable, add _ block to it to modify 50 void (^ block5) (void) =={ 51 d = d + 2; 52 NSLog (@ "c: % d, d: % d", c, d); 53}; 54 block5 (); 55} 56 return 0; 57}

 

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.