//Block 代码块
//4种类型Block
//声明格式:返回值类型 (^block名)(参数)
//1--无参,无返回值
//block的声明
void(^block1)(void);
//block的赋值
block1 = ^{
NSLog(@"hello world");
};
//block的使用
block1();
//2--有参数,无返回值
/* void (^block2)(int a);
block2 = ^(int a ){
NSLog(@"%d",a);
}; */
void (^block2)(int a) = ^(int a ){
NSLog(@"%d",a);
};
block2(99);
//3--无参数 有返回值
int (^block3)(void) = ^{
return 88;
};
int num = block3();
NSLog(@"num = %d",num);
//4--有参数,有返回值
int (^block4)(int b) = ^(int b){
return b;
};
int num2 = block4(100);
NSLog(@"num2 = %d",num2);
//-------------------------------------------------------------
int (^findMax) (int p,int q)= ^ (int p,int q){
//return p >q ? p : q;
if (p > q) {
return p;
}else{
return q;
}
};
int max = findMax(1,2);
NSLog(@"max = %d",max);
//block中 只能使用全局变量或者静态变量
//__block =static
static int number = 100;
void (^block6)(void) = ^{
number = 20;
number2 = 10;
};
block6();
NSLog(@"%d,%d",number,number2);
/