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];