转自:http://ff20081528.iteye.com/blog/1670433
一、什么是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的几个小例子
- #import <Cocoa/Cocoa.h>
-
-
- int main(int argc, char *argv[])
- {
- @autoreleasepool {
- NSLog(@"Hello world");
- void (^myblocks) (void) = NULL;
- myblocks = ^(void) {
- NSLog(@"in blocks");
- };
- NSLog(@"before myblocks");
- myblocks();
- NSLog(@"after myblocks");
-
-
- int (^myblocks2) (int a, int b) = ^(int a, int b) {
- int c = a + b;
- return c;
- };
- NSLog(@"before blocks2");
- int ret = myblocks2(10, 20);
- NSLog(@"after blocks2 ret %d", ret);
-
-
- __blockint sum = 0;
- int (^myblocks3) (int a, int b) = ^(int a, int b) {
- sum = a + b;
- return3;
- };
- myblocks3(20, 30);
- NSLog(@"sum is %d", sum);
- }
- returnNSApplicationMain(argc, (constchar **)argv);
- }
打印结果如下
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
- #import <Foundation/Foundation.h>
-
-
- @interface Dog : NSObject {
- int _ID;
- NSTimer *timer;
- int barkCount;
-
-
- void (^BarkCallback) (Dog *thisDog, int count);
- }
- @property (assign) int ID;
-
-
-
- -(void) setBark:( void (^) (Dog *thisDog, int count) ) eachBark;
-
-
- @end
2、Dog.m
- #import "Dog.h"
-
-
- @implementation Dog
- @synthesize ID = _ID;
-
-
- -(id) init
- {
- self = [superinit];
- if (self) {
-
- timer = [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfselector:@selector(updateTimer:) userInfo:nilrepeats:YES];
-
- }
- returnself;
- }
-
-
- -(void) updateTimer:(id) arg
- {
- barkCount ++;
- NSLog(@"dog %d bark count %d", _ID, barkCount);
-
- if (BarkCallback) {
-
- BarkCallback(self, barkCount);
- }
- }
-
-
-
-
- -(void) setBark:(void (^)(Dog *, int))eachBark
- {
- [BarkCallbackrelease];
- BarkCallback = [eachBark copy];
- }
-
-
- -(void) dealloc
- {
- [BarkCallbackrelease];
- [superdealloc];
- }
- @end
3、Person.h
- #import <Foundation/Foundation.h>
- #import "Dog.h"
-
-
- @interface Person : NSObject
- {
- Dog *_dog;
- }
-
-
- @property (retain) Dog *dog;
-
-
- @end
4、Person.m
- #import "Person.h"
-
-
- @implementation Person
- @synthesize dog = _dog;
-
-
- -(void) setDog:(Dog *)dog
- {
- if (_dog != dog) {
- [_dogrelease];
- _dog = [dog retain];
-
- [_dogsetBark:^(Dog *thisDog, int count) {
- NSLog(@"person dog %d count %d", [thisDog ID], count);
- }];
- }
- }
-
-
- -(Dog *) dog
- {
- return_dog;
- }
-
-
- -(void) dealloc
- {
- self.dog = nil;
- [superdealloc];
- }
-
-
- @end
5、Main.m
- #import <Foundation/Foundation.h>
- #import "Person.h"
- #import "Dog.h"
-
-
- int main(int argc, constchar * argv[])
- {
-
-
- @autoreleasepool {
-
-
- NSLog(@"Hello, World!");
- Person *person = [[Personalloc] init];
- Dog *dog = [[Dogalloc] init];
- [dog setID:10];
- [person setDog:dog];
- [dog release];
- while (1) {
- [[NSRunLoopcurrentRunLoop] run];
- }
- [person release];
-
- }
- return 0;
- }
6、打印结果
2012-09-03 11:21:08.551 blockDelegate[549:403] Hello, World!
2012-09-03 11:21:09.554 blockDelegate[549:403] dog 10 bark count 1
2012-09-03 11:21:09.555 blockDelegate[549:403] person dog 10 count 1
2012-09-03 11:21:10.554 blockDelegate[549:403] dog 10 bark count 2
2012-09-03 11:21:10.555 blockDelegate[549:403] person dog 10 count 2
2012-09-03 11:21:11.553 blockDelegate[549:403] dog 10 bark count 3
2012-09-03 11:21:11.554 blockDelegate[549:403] person dog 10 count 3
2012-09-03 11:21:12.554 blockDelegate[549:403] dog 10 bark count 4
2012-09-03 11:21:12.555 blockDelegate[549:403] person dog 10 count 4
2012-09-03 11:21:13.553 blockDelegate[549:403] dog 10 bark count 5
2012-09-03 11:21:13.553 blockDelegate[549:403] person dog 10 count 5
2012-09-03 11:21:14.553 blockDelegate[549:403] dog 10 bark count 6
2012-09-03 11:21:14.554 blockDelegate[549:403] person dog 10 count 6