//Block 代碼塊變量
//4種類型Blockstatic
//聲明格式:返回值類型 (^block名)(參數)block
//1--無參,無返回值return
//block的聲明參數
void(^block1)(void);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);
/