首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Block使用方法详解一

2013-10-29 
Block使用方法详解1第一、综述block是OC中另外一种对象和对象的通信方式,是一对一的关系,类于delegate,而通

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










热点排行