Block使用方法详解1
第一、综述
block是OC中另外一种对象和对象的通信方式,是一对一的关系,类似于delegate,而通知时一对多的关系
第二、定义block类型
int (^myBlock)(int)
第三、block的声明
mylock=^(int a)
{
int result =a*a;
return result;
}
第四、block的调用
block(20);
code sample如下所示:
__block int num=10; MyBlock myblock=^(int a) { NSLog(@"before the val of num is %d",num); num++; return 30; }; num=30; [self testBlock:myblock]; NSLog(@"after the val of num is %d",num);输出结果是:
2013-10-28 15:38:24.602 blockDemo[1541:70b] before the val of num is 30
2013-10-28 15:38:24.603 blockDemo[1541:70b] after the val of num is 31