block类型在Xcode编译器中输入inlineBlock快速创建。
用法:
无参无返回值的block
void(^myBlock)() = ^() {有参无返回值
NSLog(@"my Block");
};
myBlock();
有返回值有参void(^myBlock1)(int x,int y) = ^(int x,int y){
NSLog(@"%d + %d = %d",x,y,x+y);
};
myBlock1(10,2);
block的自定义类型int(^myBlock3)(int x,int y) = ^(int x,int y){
return x+y;
};
int x = myBlock3(2,66);
NSLog(@"x = %d",x);
//自定义类型
typedef void(^myBlock4)();
//声明一个变量
myBlock4 block = ^(){
NSLog(@"typedef block");
};
//调用block
block();
typedef int(^myblock5) (int x, int y);
myblock5 block1 = ^(int x, int y){
return x>y ? x :y;
};
NSLog(@"block1 = %d",block1(43,64));