block 構成部分
^(參數(shù)列){行為主體}躯护; ? ? ? ? ??例如:^(int a) {return a*a};
block Pointer
回傳值(^名字)(參數(shù)列)钞螟;
int (^ square ) (int) ;
以上是一個bool型的變量
//定義一個bool類型的block變量拥刻;
BOOL(^isInputEven)(int) = ^(intinput){
if(input%2==0) {
returnYES;
}else{
returnNO;
}
};
//調用block
NSString*number = isInputEven(index) ?@"is an even":@"is not even";
NSLog(@"-----%@",number);//應該輸出 is not even
//block外的price能在block內部使用
floatprice =1.99;
float(^finalPrice)(int) = ^(intquantity){
//在block內的price是readonly的
returnquantity * price;
};
intorderQuantity =10;
floatfinal = finalPrice(orderQuantity);
NSLog(@"final--------%f",final);
講局部變量聲明為__block,表示外部變化將在block內進行同樣操作狭瞎,比如: