1、block简单示例://一、在单个类中实现//1.声明—》》返回值类型(^block名)(参数类型1,参数类型2);int(^block1)(int,int);//2.实现—^(参数类型1参数1,参数类型2参数2){…};block1=^(inta,intb){returna+b;};//3.调用intresult=block1(1,2);NSLog(@result=%d,result);//二、void类型的block//1.声明void(^block2)();//2.实现/*实现方法1block2=^{//...};*///实现方法2block2=^(){NSLog(@空类型block);};//3.调用blockblock2();//三、定义block类型//定义block类型--返回值类型(^block类型)(参数类型1,参数类型2);typedefint(^SumBlock)(int,int);//1.声明blockSumBlocksumBlock;//2.实现sumBlock=^(inta,intb){returna+b;};//3.调用intresult=sumBlock(1,2);NSLog(@%d,result);2.block作为参数//一、整型返回值block做参数TestViewController.h文件中//整型返回值block--(返回值类型(^)(参数类型1,参数类型2))block名//1.声明-(int)testBlock1:(int(^)(int,int))block;TestViewController.m文件中//实现方法-(int)testBlock1:(int(^)(int,int))block{//3.调用blockintresult=block(1,2);returnresult;}ViewController.m文件中TestViewController*testVC=[[TestViewControlleralloc]init];//2.调用方法,实现block—》实现block时,参数要给定形参变量intresult=[testVCtestBlock1:^int(inta,intb){returna+b;}];NSLog(@result1=%d,result);//二、空类型block做参数TestViewController.h文件中//空类型block—(返回值类型(^)())block名;//1.声明-(void)testBlock2:(void(^)())block;TestViewController.m文件中//实现方法-(void)testBlock2:(void(^)())block{//3.调用blockblock();}ViewController.m文件中TestViewController*testVC=[[TestViewControlleralloc]init];//2.实现block[testVCtestBlock2:^{NSLog(@voidblock实现);}];//三、定义block类型做参数TestViewController.h文件中//定义block类型typedefint(^TestBlock)(int,int);//block类型-(void)testBlock3:(TestBlock)testBlock;TestViewController.m文件中//实现方法-(void)testBlock3:(TestBlock)testBlock{//3.调用blockintresult=testBlock(2,3);NSLog(@result=%d,result);}ViewController.m文件中TestViewController*testVC=[[TestViewControlleralloc]init];//2.实现block[testVCtestBlock3:^int(inta,intb){returna+b;}];3.block修改局部变量//block引用局部变量-(void)blockConst{__blockinta=10;//1.声明void(^block)();//2.实现block=^(){//block改变局部变量必须在局部变量声明前加上__blocka=20;NSLog(@a=%d,a);};//3.调用block();}4.block引用局部变量和实例变量的引用计数问题,引用计数都会在原来基础上+1-(void)blockCount{//引用计数为1__blockNSObject*obj=[[NSObjectalloc]init];//1.声明void(^block)();//2.实现block=^(){//在MRC中,obj此时引用计数为2,block调用引用计数加1//NSLog(@%ld,obj.retainCount);NSLog(@obj=%@,obj);};//3.调用block();}详情关注新浪微博: