The usage of block in OC as the method parameter, and the usage of the ocblock Parameter

Source: Internet
Author: User

The usage of block in OC as the method parameter, and the usage of the ocblock Parameter

Method 1: directly declare the block callback method when passing parameters.

1. Definition method:

-(Int) doTest :( NSString *) name success :( int (^) (int param1, int param2) success para1 :( int) temp1 para2 :( int) temp2

{

Int ret = success (temp1, temp2 );

NSLog (@ "---- % @ -- % d --", name, ret );

Return ret;

}

2. Call method:

[Self doTest: @ "Tom" success: ^ int (int param1, int param2) {// when doTest is called, this red block will be called immediately in doTest (see the blue code above ).

Return param1 + param2;

} Para1: 8 para2: 15];

 

========================================================== ===

Method 2: Use typedef to define a block alias. Use the alias to declare that the parameter is of the block type.

1. Declare the type of a block method:

Typedef int (^ myBlock) (int param1, int param2 );

 

2. Definition method:

-(Int) doTest :( NSString *) name success :( myBlock) success para1 :( int) temp1 para2 :( int) temp2

{

Int ret = success (temp1, temp2 );

NSLog (@ "---- % @ -- % d --", name, ret );

Return ret;

}

3. Call method:

[Self doTest: @ "Tom" success: ^ int (int param1, int param2 ){

Return param1 + param2;

} Para1: 8 para2: 15];

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.