ios学习笔记之block在ios开发中的应用

时间:2024-12-08 15:05:44

一、什么是Blocks      Block是一个C级别的语法以及运行时的一个特性,和标准C中的函数(函数指针)类似,但是其运行需要编译器和运行时支持,从ios4.0开始就很好的支持Block。
二、在ios开发中,什么情况下使用Block      Block除了能够定义参数列表、返回类型外,还能够获取被定义时的词法范围内的状态(比如局部变量),并且在一定条件下(比如使用__block变量)能够修改这些状态。此外,这些可修改的状态在相同词法范围内的多个block之间是共享的,即便出了该词法范围(比如栈展开,出了作用域),仍可以继续共享或者修改这些状态。通常来说,block都是一些简短代码片段的封装,适用作工作单元,通常用来做并发任务、遍历、以及回调。
三、block如何申明(对比于c语言中的函数申明) ios学习笔记之block在ios开发中的应用四、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. }
#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); //此处如果不加__block会报错
__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

  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
#import <Foundation/Foundation.h>

@interface Dog : NSObject {
int _ID;
NSTimer *timer;
int barkCount; //定义一个blocks变量
void (^BarkCallback) (Dog *thisDog, int count);
}
@property (assign) int ID; //向外暴露一个接口
-(void) setBark:( void (^) (Dog *thisDog, int count) ) eachBark; @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
#import "Dog.h"

@implementation Dog
@synthesize ID = _ID; -(id) init
{
self = [superinit];
if (self) {
//每隔1s调用一次updateTimer方法
timer = [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfselector:@selector(updateTimer:) userInfo:nilrepeats:YES]; }
returnself;
} -(void) updateTimer:(id) arg
{
barkCount ++;
NSLog(@"dog %d bark count %d", _ID, barkCount);
//向Person对象进行汇报
if (BarkCallback) {
//调用从Person传过来的Blocks
BarkCallback(self, barkCount);
}
} -(void) setBark:(void (^)(Dog *, int))eachBark
{
[BarkCallbackrelease];
BarkCallback = [eachBark copy];
} -(void) dealloc
{
[BarkCallbackrelease];
[superdealloc];
}
@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
#import <Foundation/Foundation.h>
#import "Dog.h" @interface Person : NSObject
{
Dog *_dog;
} @property (retain) Dog *dog; @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
#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

  1. #import <Foundation/Foundation.h>
  2. #import "Person.h"
  3. #import "Dog.h"
  4. int main(int argc, constchar * argv[])
  5. {
  6. @autoreleasepool {
  7. // insert code here...
  8. NSLog(@"Hello, World!");
  9. Person *person = [[Personalloc] init];
  10. Dog *dog = [[Dogalloc] init];
  11. [dog setID:10];
  12. [person setDog:dog];
  13. [dog release];
  14. while (1) {
  15. [[NSRunLoopcurrentRunLoop] run];
  16. }
  17. [person release];
  18. }
  19. return 0;
  20. }
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Dog.h" int main(int argc, constchar * argv[])
{ @autoreleasepool { // insert code here...
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