#import <Foundation/Foundation.h>
//定义一个别名 BlockType
typedef void (^BlockType)();
typedef void (^BlockType1)(int a,int b);
typedef int (^BlockType2)(int a,int b);
//定义返回值是BlockType类型的函数
BlockType test(){
//定义block变量 b1
void (^b1)() = ^{
int a = 10;
int s = a+100;
NSLog(@"s = %d",s);
};
//b1();
return b1;
}
//定义返回值是BlockType1的函数
BlockType1 test1(){
return ^(int a,int b){
NSLog(@"a+b = %d",a+b);
};
}
BlockType2 test2(){
//return 函数返回值
return ^(int a,int b){
//return block代码块的返回值
return a+b;
};
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
//定义BlockType 类型的变量,接收test函数返回的结果i
BlockType bb = test();
bb(); //执行 block
BlockType1 b2 = test1();
b2(34,10);
BlockType2 b3 = test2();
//b3接收了函数的返回值
//因为函数的返回值是一个有参数,有返回值的block
//所以,b3可以直接执行block,同时block返回值是int类型
//故,s = b3(10,38);
int s = b3(10,38);
NSLog(@"s = %d",s);
}
return 0;
} |
|