一、什么是Blocks 
     Block是一个C级别的语法以及运行时的一个特性,和标准C中的函数(函数指针)类似,但是其运行需要编译器和运行时支持,从ios4.0开始就很好的支持Block。

二、在ios开发中,什么情况下使用Block 
     Block除了能够定义参数列表、返回类型外,还能够获取被定义时的词法范围内的状态(比如局部变量),并且在一定条件下(比如使用__block变量)能够修改这些状态。此外,这些可修改的状态在相同词法范围内的多个block之间是共享的,即便出了该词法范围(比如栈展开,出了作用域),仍可以继续共享或者修改这些状态。通常来说,block都是一些简短代码片段的封装,适用作工作单元,通常用来做并发任务、遍历、以及回调。

三、block如何申明(对比于c语言中的函数申明)

四、c函数指正和blocks调用 
     int (*CFunc) (int a) 函数调用 
     int result = CFunc(10); 
     int (^BFunc)  (int  a)  函数调用 
     int result = BFunc(10);

五、__block  关键字 
     一个Block的内部时可以引用自身作用域外的变量的,包括static变量,extern变量或自由变量(定义一个变量的时候,如果不加存储修饰符,默认情况下就是自由变量auto,auto变量保存在stack中的。除了auto之外还存在register,static等存储修饰符),对于自由变量,在Block中只读的。在引入block的同时,还引入了一种特殊的__block关键字变量存储修饰符。

六、block的几个小例子

  1. #import <Cocoa/Cocoa.h>
  2. int main(int argc, char *argv[])
  3. {
  4. @autoreleasepool {
  5. NSLog(@"Hello world");
  6. void (^myblocks) (void) = NULL;
  7. myblocks = ^(void) {
  8. NSLog(@"in blocks");
  9. };
  10. NSLog(@"before myblocks");
  11. myblocks();
  12. NSLog(@"after myblocks");
  13. int (^myblocks2) (int a, int b) = ^(int a, int b) {
  14. int c = a + b;
  15. return c;
  16. };
  17. NSLog(@"before blocks2");
  18. int ret = myblocks2(10, 20);
  19. NSLog(@"after blocks2 ret %d", ret);
  20. //此处如果不加__block会报错
  21. __blockint sum = 0;
  22. int (^myblocks3) (int a, int b) = ^(int a, int b) {
  23. sum = a + b;
  24. return3;
  25. };
  26. myblocks3(20, 30);
  27. NSLog(@"sum is %d", sum);
  28. }
  29. returnNSApplicationMain(argc, (constchar **)argv);
  30. }

打印结果如下 
2012-09-03 10:23:20.878 blockTest[407:403] Hello world 
2012-09-03 10:23:20.880 blockTest[407:403] before myblocks 
2012-09-03 10:23:20.881 blockTest[407:403] in blocks 
2012-09-03 10:23:20.881 blockTest[407:403] after myblocks 
2012-09-03 10:23:20.882 blockTest[407:403] before blocks2 
2012-09-03 10:23:20.882 blockTest[407:403] after blocks2 ret 30 
2012-09-03 10:23:20.882 blockTest[407:403] sum is 50

七、block写的回调例子 
1、Dog.h

  1. #import <Foundation/Foundation.h>
  2. @interface Dog : NSObject {
  3. int _ID;
  4. NSTimer *timer;
  5. int barkCount;
  6. //定义一个blocks变量
  7. void (^BarkCallback) (Dog *thisDog, int count);
  8. }
  9. @property (assign) int ID;
  10. //向外暴露一个接口
  11. -(void) setBark:( void (^) (Dog *thisDog, int count) ) eachBark;
  12. @end

2、Dog.m

  1. #import "Dog.h"
  2. @implementation Dog
  3. @synthesize ID = _ID;
  4. -(id) init
  5. {
  6. self = [superinit];
  7. if (self) {
  8. //每隔1s调用一次updateTimer方法
  9. timer = [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfselector:@selector(updateTimer:) userInfo:nilrepeats:YES];
  10. }
  11. returnself;
  12. }
  13. -(void) updateTimer:(id) arg
  14. {
  15. barkCount ++;
  16. NSLog(@"dog %d bark count %d", _ID, barkCount);
  17. //向Person对象进行汇报
  18. if (BarkCallback) {
  19. //调用从Person传过来的Blocks
  20. BarkCallback(self, barkCount);
  21. }
  22. }
  23. -(void) setBark:(void (^)(Dog *, int))eachBark
  24. {
  25. [BarkCallbackrelease];
  26. BarkCallback = [eachBark copy];
  27. }
  28. -(void) dealloc
  29. {
  30. [BarkCallbackrelease];
  31. [superdealloc];
  32. }
  33. @end

3、Person.h

  1. #import <Foundation/Foundation.h>
  2. #import "Dog.h"
  3. @interface Person : NSObject
  4. {
  5. Dog *_dog;
  6. }
  7. @property (retain) Dog *dog;
  8. @end

4、Person.m

  1. #import "Person.h"
  2. @implementation Person
  3. @synthesize dog = _dog;
  4. -(void) setDog:(Dog *)dog
  5. {
  6. if (_dog != dog) {
  7. [_dogrelease];
  8. _dog = [dog retain];
  9. [_dogsetBark:^(Dog *thisDog, int count) {
  10. NSLog(@"person dog %d count %d", [thisDog ID], count);
  11. }];
  12. }
  13. }
  14. -(Dog *) dog
  15. {
  16. return_dog;
  17. }
  18. -(void) dealloc
  19. {
  20. self.dog = nil;
  21. [superdealloc];
  22. }
  23. @end

5、Main.m

    1. #import <Foundation/Foundation.h>
    2. #import "Person.h"
    3. #import "Dog.h"
    4. int main(int argc, constchar * argv[])
    5. {

最新文章

  1. Java Spring mvc 操作 Redis 及 Redis 集群
  2. nginx.conf的events,http段一般固定配置
  3. Statement returned more than one row, where no more than one was expected
  4. Hadoop could not find or load main class
  5. 交流异步电机的Modelica模型
  6. hadoop(五): shell命令
  7. 辛星解读mysql的用户管理
  8. 进入IT行业四月后的感想(生活日志)欢迎评论
  9. 微信公众号的开发 Senparc.Weixin.dll使用
  10. css3学习系列之移动
  11. python网页爬虫开发之三
  12. learning at command AT+CEREG
  13. Codeforces.838D.Airplane Arrangements(思路)
  14. [BZOJ1122][POI2008]账本BBB 单调队列+后缀和
  15. hdu 2896 AC自动机模版题
  16. 【300】◀▶ IDL - ENVI API
  17. .NET控件命名规范
  18. STM32(9)——通用定时器作为输入捕捉
  19. .getClass()和.class的区别
  20. Android C语言_init函数和constructor属性及.init/.init_array节探索

热门文章

  1. H.264的一些资料整理
  2. iOS Foundation框架 -3.利用NSNumber和NSValue将非OC对象类型数据存放到集合
  3. HttpWebResponse取不到Cookie?原来是因为被跳转了
  4. 《mysql数据库备份小脚本》
  5. jquery.validate.js
  6. IOS_修改项目模板
  7. php学习日志(5)-解决Windows Live Writer错误:WindowsLive.Writer.CoreServices.HttpRequestHelper的类型初始值设定发生异常
  8. 数据库操作类util
  9. MVVM模式的一个小例子
  10. JSON对象配合jquery.tmpl.min.js插件,手动攒出一个table