1.Block的格式:
<returntype> (^blockname)(list of arguments) = ^(arguments){ body; };
没有返回类型和参数的例子:
void (^myFirstBlock)(void)=^(void){NSLog(@"what is your name");};
有参数的例子:
void (mySecondBlock)(int number)=^(int number){NSLog(@"you have entered %d",number);};
有参数有返回值的例子:
int (^myThirdBlock)(int aNumber)=^(int aNumber){return aNumber;};
直接使用,跳过声明的例子:
(直接使用的例子有待于进一步研究)
下面讲typedef的例子
typedef int (^myThirdBlock)(int aNumber);//用typedef来定义一整个具有返回值类型和参数的系列
myThirdBlock theRealThirdBlock=^(int aNumber){return aNumber;};//声明的时候直接就可以用定义好的系列来生成一个该系列的实例,名字重新选取,然后给出具体的实现。
NSLog(@"Here is the second number: %d",theRealThirdBlock(3));//block可以直接被当成任何内容来使用,其本身现在就相当于是一个int型的实例变量。