1.block 语法 (block 语句)
a.//全
^int(int count){return count+1;} ^void(void){return count+1;}
b.//省略返回值
^(int count){return count+1;}
c.//省略返回值和参数
^{printf(@"test\n");}
2.block类型变量
//全
int (^blk)(int) = ^int (int count){return count+1;};
3.block类型变量作为函数参数使用
a.//block作为函数参数使用
//法一
void func(int (^blk)(int)); //c的写法
+ (void)getQNToken:(void(^)(NSDictionary *dic))success; //oc写法
//法二推荐 定义类型在使用
typedef int (^blk_t)(int);
void func1(blk_t blk);
typedef void(^SuccessBlock)(NSDictionary *dic);//把上面类型拿过来添加一个type就可以
+ (void)success:(SuccessBlock)success;
b.//block 作为函数返回值使用
int (^func2())(int) //该语句是个函数 //注意把func后面的()去掉就是声明block变量
{
return ^(int count){return count+1;};
}
//推荐用如下方式
blk_t func2()
{
return ^(int count){return count+1;};
}
如果你发现本文对你有所帮助,如果你认为其他人也可能受益,请把它分享出去。