iOS pointer to function and block

Source: Internet
Author: User

One: block Basics

Block Basics

Basic concept: block is used to save a piece of code; ^: is a block sign like *: A sign of a pointer

Features: 1: Save a piece of code;

2: can have parameters and return values;

3: Can be passed as a function parameter;

and code blocks, the code in the code will be executed automatically, block the code to be called manually;

Two: Common data types, pointers to functions, and the definition of block

1: Basic Data type:

For example:int a = 10;

Format: data type variable name = value;

2: Pointer to function: can be modeled on the definition of the underlying data type above

For example:void (*p) () = function; (Funciton is a well-defined function)

Format: function type * pointer variable name = function name;

Note: Since the P pointer is a pointer to a function, the red parentheses in (*p)() must be there, because the function itself is to be parameterized, even if no parameters are written ();

If the function p points to has parameters:int (*pp) (int, int) = SUM;

To invoke a pointer to a function:

No reference: P ();

parameter: int sum = PP (2,3);

The definition and use of 3:block, and the pointer to the function of the format very much like

For example:void (^firstblock) () = ^(){...   Code ...}; Description: The blue parenthesis can not be written, the parameters must be written when

Format:block type ^block name = ^{Code snippet};

Parameter block:int (^multiblock) (int, int) = ^ (int a, int b) {

return a * b;

};

  Call BLOCK: No parameter:Firstblock ()

parameter: int result = Multiblock (2, 3);

  

// Note: Functions and blocks that point to pointers can be defined in steps like basic data types: int (^multiblock) (intint);         = ^ (intint  b) {        return A * b;    };

Example code:

1 //function Definition2 intSumintAintb)3 {4NSLog (@"a+b=%d", A +b);5     return 2;6 }7 8 voidlogsome ()9 {TenNSLog (@"I am the output function of void type"); One } A  - intMain () - { the     /** * 1: Definition of basic data type * **/ -     intA =Ten; -     //1.1 Define and assign values first -     intb; +b = -; -      +     /** * 2: pointer to function * **/ A      at     //2.1: Pointer to the sum (int a, int b) function with parameters -     int(*p) (int,int) =sum; -     //2.2: Pointer to parameterless function logsome () function -     void(*LOGP) () =Logsome; -      -     //call a pointer function to a function inP2,3); - Logp (); to      +     /** * 3:block definition and use * **/ -      the     //3.1: Block definition with no parameters *     void(^firstblock) () = ^{ $         Panax NotoginsengNSLog (@"Note: (^multiblock) (), here the parentheses must be written, regardless of whether there are parameters"); -          theNSLog (@"the latter side of the \"^(){ \"the parentheses can not be written"); +     }; A      the     //Call of Block + Firstblock (); -      $     //3.2 block with parameters and return values $     int(^multiblock) (int,int) = ^(intAintb) { -         returnAb; -     }; the      -     //Call BlockWuyi     intresult = Multiblock (2,3); theNSLog (@"%d", result); -      Wu}

Three: Using a typedef-defined block

Using typedef can simplify the tedious block definition

For example: typedef void (^myblock) (); You can then create a block with Myblock, create a format like class to create the object

  

// define block with typedef void (^myblock) (); int Main () {    //  use a custom block to create a block    myblock myblock = ^{        NSLog ( @"" );    };    }

Four: block and pointers to functions as function arguments

Using typedef to make the cumbersome block simple, this block becomes a type that can create objects like classes, and pointers to functions can also

//defining pointers to functions with typedef definitionstypedefvoid(*Tp) ();//define block with typedeftypedefvoid(^Myblock) ();voidLogsome () {NSLog (@"I am the output function of void type");}//pointer to function as argumentvoidtest1 (Tp p) {p ();}//block as a parameter of a functionvoidtest2 (myblock MB) {MB ();}intMain () {//pointer to function definitionTp MyP =Logsome;        Test3 (MyP); //Define blockMyblock MB = ^{NSLog (@"Block Output");    }; Test4 (MB);}

Note: C language functions and methods in OC are somewhat different, C language function parameters to write block the latter pointer to the function, you must first use the TypeDef bar block or pointer to the function of pointers defined as a type, and then can be passed in the parameters of the function;

In the OC method definition, block can be used as the parameter directly;

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.