iOS Learning Note---OC language Sixth day

Source: Internet
Author: User

Block, Array advanced

Block is essentially an anonymous function (a function without a name)

Block syntax is similar to function pointers

Retrospective function

Function: C language, a code snippet that implements a class of functionality.

The complete function consists of two parts: function declaration, function definition

function declaration, which is the function prototype. For example: int sum (int x,int y), a function with two integer parameters and an integral type return value.

function definition, which is the function implementation. For example: int sum (int x,int y) {
Retrun x + y;
}

Review function pointers

function pointer (variable): A pointer variable that holds the address of a function (the name of a functor).

Int (*p) (int x,int y) = sum;

function pointer type: INT (*) (int x,int y) is a pointer to two integer parameters, an integer that returns a value function.

function pointer variable: The value of the P function pointer: sum

Block

anonymous function: A function without a name. such as int (int x, int y)

Because block is an anonymous function, the implementation of the function that the block variable holds can be directly adjusted by the block variable. function

How does a function with no name be tuned? (If you have a name, you can adjust it directly, and

Can be adjusted by a function pointer)

    int(^BLOCK1) (int,int) = ^int(intXinty) {returnX +y;    }; //type int (^) (int,int)//variable name Block1//Initial value//^int (int x,int y)//    {//return x + y;//    }typedefint(^blocktype) (int,int); Blocktype Block2= ^int(intXinty) {        returnX *y;    }; //the characteristic of assigning a value to a block variable: the definition of the function (implemented), minus the name of the letter

The return value type in the block function definition can be omitted and will usually be omitted

1     int(^AA) () = ^(){2         return Ten;3     };4     //You can omit the function definition () when the block has no parameters.5 //Int (^AA) () = ^{6 //return ten;7 //    };8NSLog (@"%d", AA ());9     void(^hello) () = ^{TenNSLog (@"Hello World"); One     }; AHello ();//block with no return value without parameters

An external variable can be used directly in the block body, but you cannot modify the variable to add __block before the variable.

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.