3、一点点扩展
由此,我们可以理解,如果block中操作的对象是指针,那么直接可以进行修改,这包括OC对象,如果不是,则需要用__block关键字修饰。
4、关于引用计数
在block中访问的对象,会默认retain:
UIImage * number;
number = [[UIImage alloc]init] ;
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)number));
block1 = ^(int a,int b){
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)number));
};
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)number));
结果如下:
而添加__block的对象不会被retain;
注意:如果我们访问类的成员变量,或者通过类方法来访问对象,那么这些对象不会被retain,而类对象会被return,最常见的时self:
typedef void(^myBlock)(int,int) ;
@interface ViewController2 ()
{
myBlock block1;
__block UIImage * number;
}
@end
@implementation ViewController2
-(void)dealloc{
NSLog(@"dealloc %@",self.class);
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)number));
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
number = [[UIImage alloc]init] ;
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)number));
block1 = ^(int a,int b){
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)number));
};
//block1(1,1);
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)number));
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(100, 100, 100, 100);
btn.backgroundColor=[UIColor redColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
-(void)click{
[self dismissViewControllerAnimated:YES completion:nil];
}
打印结果:
可以看出,UIImage对象没有被retain,而self也将循环引用,造成内存泄露。解决方法如下:
number = [[UIImage alloc]init] ;
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)number));
UIImage * im = number;
block1 = ^(int a,int b){
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)im));
};
NSLog(@"%ld",CFGetRetainCount((__bridge CFTypeRef)number));
打印结果:
注意:根据这个机制,如果我们将block用来传值,在block不用时,务必要置为nil,而在实现block的方法里,务必要释放;我们通过代码来解释:
首先,创建三个ViewController,为ViewController1,ViewController2,ViewController3;
1、在ViewController1中创建一个按钮,跳转ViewController2
2、在ViewController2中:
#import "ViewController2.h"
#import "ViewController3.h"
@interface ViewController2 ()
{
UIButton * im;
}
@end
@implementation ViewController3
-(void)dealloc{
NSLog(@"dealloc %@",self.class);
}
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(300, 300, 100, 100);
btn.backgroundColor=[UIColor redColor];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
im = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
im.backgroundColor=[UIColor blackColor];
[im addTarget:self action:@selector(rele) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:im];
}
-(void)rele{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)click{
ViewController3 * con = [[ViewController3 alloc]init];
[con setBlock:^{
im.backgroundColor=[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
}];
[self presentViewController:con animated:YES completion:nil];
}
3、在ViewController3中:
#import "ViewController3.h"
void (^myBlock)();
@implementation ViewController3
-(void)setBlock:(void(^)())block{
myBlock = [block copy];
}
-(void)dealloc{
NSLog(@"dealloc %@",self.class);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
myBlock();
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(100, 100, 100, 100);
btn.backgroundColor=[UIColor redColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
-(void)click{
[self dismissViewControllerAnimated:YES completion:nil];
}
通过打印信息,我们会发现,ViewController2不被释放,原因是其成员变量im被block中retain没有释放,我们这样做:
@interface ViewController2 ()
{
UIButton * im;
ViewController3 * tem;
}
-(void)rele{
[tem setBlock:nil];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)click{
ViewController3 * con = [[ViewController2 alloc]init];
tem=con;
[con setBlock:^{
im.backgroundColor=[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
}];
[self presentViewController:con animated:YES completion:nil];
}
这样就解决了内存问题。
四、关于block的作用域
应避免将花括号中的block用于外面,如果需要,你可以将这个block声明为全局的。