Block, blockb

Source: Internet
Author: User

Block, blockb

// 1 declare block

// 2 implement block

// 3 call the block

// Declare the function pointer

// Void (* ptest1) (void)

// Declare the block to point to an anonymous Function

Void (^ block1) (void );

Int (^ block2) (int a, int B );

Int (^ block3) (int a, int B );

Typedef void (^ block4) (void );

Typedef int (^ block5) (int a, int B );

 

// Block implementation

// Block1 points to an anonymous function. An anonymous function must end with a semicolon. If the block parameter type is void, the parameters of the anonymous function to be pointed can be omitted without writing. Other parameters cannot be omitted.

// The returned value of a block pointing to an anonymous function can be omitted (all types of return values can be omitted)

Block1 = ^

{
Printf ("block1 \ n ");

};

// The anonymous function is used to calculate the sum of a + B

Block2 = ^ (int a, int B)

{

Return a + B;

};

Block3 = ^ (int a, int B)

{
Return a-B;

};

// The role of bl4 is block4

Block4 bl4;

Bl4 = ^

{
NSLog (@ "block4 ");

};

Block5 bl5;

Bl5 = ^ (int a, int B)

{
Return a/B;

 

};

// Block call

Block1 ();

Int sum = block2 (2, 5 );

Printf ("% I \ n", sum );

Int mut = block3 (5, 3 );

Printf ("% I \ n", mut );

Bl4 ();

NSLog (@ "% I", bl5 (8, 2 ));

######################################## #########

Void test1 (void)

{

Printf ("test1 \ n ");

}

Void test2 (void)

{

Printf ("test2 \ n ");

}

Int test3 (int a, int B)

{

Return a + B;

}

Int test4 (int a, int B)

{

Return a-B;

}

 

(1) // call the C function by function name

Test1 ();

(2) // declare the function pointer. The function pointer points to the test1 function.

// Function pointer can point to function

Void (* ptest1) (void );

Ptest1 = test1;

// Call a function through a pointer

Ptest1 ();

Void (* ptest2) (void );

Ptest2 = test2;

Ptest2 ();

// Call by function name

Int result = test3 (2, 5 );

Printf ("% d", result );

// Call with function pointer

Int (* ptest3) (int a, int B );

Ptest3 = test3;

Int result = ptest3 (2, 5 );

Printf ("% d", result );

Int (* ptest4) (int a, int B );

Ptest4 = test4;

Int sum = ptest4 (5, 2 );

Printf ("% d", sum );

// Typedef custom type

Typedef int MYINT;

MYINT I = 10;

Printf ("% d", I );

 

Typedef void (* tdftest1) (void );

Tdftest1 t1 = test1;

// T1 = test1;

T1 ();

Typedef int (* tdfTest3) (int a, int B );

TdfTest3 t3 = test3;

Int a = t3 (5, 2 );

Printf ("% d", );

// Practice

There is a warning when calling some controls in the block. You can use the following method to release the warning:

_ Block UILabel * label = self. label;

_ SecondController. bl = ^ (float size)

{
Label. font = [UIFont systemFontOfSize: size];

};

 

# Import <UIKit/UIKit. h>

 

// Declare block

Typedef void (^ block) (float size );

 

 

 

@ Interface SecondViewController: UIViewController

-(IBAction) pressSteper :( id) sender;

 

// Use strong to encapsulate attributes for a block

// If the block attribute is encapsulated, the declared block must be modified with typedef.

@ Property (strong, nonatomic) block bl;

 

 

 

 

@ End

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.