Objective-C: 09_block

Source: Internet
Author: User
Block is used to save a piece of code. Block ID: ^Blick is similar to a function. 1. It can save code segments. 2. It has returned values. 3. It has parameters. 4. It has the same calling method. Block definition: Return Value Type (^ block name) (parameter type list) = ^ (parameter list) {code segment }; Block call: Block name (parameter list );  INT (^ sumblock) (INT, INT) = ^ (int A, int B ){ Return A + B; };Define block variables: INT (^ sumblock) (INT, INT)Use block to encapsulate the Code: ^ (Int A, int B) { Return A + B; };Block access to external variables: 1. The block can access external variables. 2. By default, external local variables cannot be modified inside the block. 3. Add the _ block keyword to the local variable (the first two underscores), and the local variable can be modified inside the block.Use typedef to define the block type: typedef int (^ myblock) (INT, INT); myblock B1, B2; myblock sumblock = ^ (int A, int B) {return a + B ;}; similar to the pointer to a function: int sum (int A, int B) {return a + B ;}function pointer: int (* P) (INT, INT) = sum; P (10, 12 );

Objective-C: 09_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.