1. Define Block
/ * return void , parameter is void block*/
void (^blockreturningvoidwithvoidargument) ( void );
/ * callback integer, two parameters are integers and character block*/
int (^blockreturningintwithintandchararguments) ( int , char );
/ * return void , array with block , each block has a parameter of type integer */
void (^arrayoftenblocksreturningvoidwinintargument[ ]) ( int );
/* Define object method use Block,block to pass in a nsstring parameter, return void*/
-(void) Testblockwithparam: (void (^) (nsstring *text)) block;
2. Use block
-(void) Testblockwithparam: (void (^) (nsstring *text)) block{
NSLog(@ "start test");
// define parameter x, use in Testx
int x = 1;
int (^TESTX) (int) = ^ (int num2) {
return x+num2;
};
NSLog (@ "Use x:%d", Testx ());
// use the __block identifier to define a parameter y that can modify the value in the block
__block int y = 2;
int (^testy) (int) = ^ (int num2) {
if (Num2 > ten) {
y = ten;
}Else{
y = 2;
}
return y+num2;
};
NSLog(@ "when num2 > 10,sum =%d", testy);
NSLog(@ "when num2 <= 10,sum =%d", testy (8));
[nsthreadsleepfortimeinterval:1.0f];
Block (@ "Success");
}