iOS 中block结构的简单用法

自从block出现以后,不少API都开始采用这样的结构,因而可知,block确实有许多优点存在,这里将一些简单用法总结以下:dom

1、如何声明一个block变量ide

咱们经过^符号来声明block类型,形式以下:函数

void (^myBlock)();指针

其中第一个void是返回值,能够是任意类型,中间括号中^后面的是这个block变量的名字,我把它命名为myBlock,最后一个括号中是参数,若是多参数,能够写成以下样式:code

int (^myBlock)(int,int);对象

一样,你也能够给参数起名字:内存

int (^myBlock)(int a,int b);作用域

不少时候,咱们须要将咱们声明的block类型做为函数的参数,也有两种方式:get

一、-(void)func:(int (^)(int a,int b))block;编译器

第二种方式是经过typedef定义一种新的类型,这也是大多数状况下采用的方式:

二、typedef int (^myBlock)(int a,int b) ;

-(void)func:(myBlock)block ;

2、如何实现一个block

既然block能够被声明为变量,那么就必定能够实现它,就像其余类型变量的赋值。我本身对block的理解为它是一断代码块,因此给它赋值赋即是一段代码段:

typedef int (^myBlock)(int,int) ;
@interface ViewController ()
{
    myBlock block1;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    block1 =^(int a, int b){
        return a+b;
    };
   NSLog(@"%d",block1(1,1));
}

这里打印的结果是2,从这里能够发现block和函数的功能很像。

注意:一、在上面的代码里 block1是一个对象,若是直接打印将打印对象地址

        二、block(),加上后面的括号才是执行block语句块

3、block中访问对象的微妙关系

一、若是你在一个block块中仅仅访问对象,而不是对他进行修改操做,是没有任何问题的:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    int tem=2;
    block1 = ^(int a,int b){
        int count= tem+1;
        return count;
    };
    NSLog(@"%d",block1(1,1));
}

而若是我在block块中直接修改,编译器会报错:

  block1 = ^(int a,int b){
        tem+=1;
        return tem+1;
    };

为何会出现这样的状况,根据猜想,多是block内部将访问的变量都备份了一份,若是咱们在内部修改,外部的变量并不会被修改,咱们能够经过打印变量的地址来证实这一点:

- (void)viewDidLoad {
    [super viewDidLoad]; 
    int tem=2;
    NSLog(@"%p",&tem);
    block1 = ^(int a,int b){
        NSLog(@"%p",&tem);
        return tem+1;
    };
    NSLog(@"%d",block1(1,1)); 
}

打印结果以下:

能够看出,变量的地址已经改变。

二、__block 作了什么

为了能够在block块中访问并修改外部变量,咱们常会把变量声明成__block类型,经过上面的原理,能够发现,其实这个关键字只作了一件事,若是在block中访问没有添加这个关键字的变量,会访问到block本身拷贝的那一份变量,它是在block建立的时候建立的,而访问加了这个关键字的变量,则会访问这个变量的地址所对应的变量。咱们能够经过代码来证实:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    int tem=2;
    block1 = ^(int a,int b){  
        return tem+a+b;
    };
    tem=4;
    NSLog(@"%d",block1(1,1));
    
    block1 = ^(int a,int b){  
        return tem+a+b;
    };
     __block int tem2=2;
    tem2=4;
     NSLog(@"%d",block1(1,1));
}

结果:

三、一点点扩展

由此,咱们能够理解,若是block中操做的对象是指针,那么直接能够进行修改,这包括OC对象,若是不是,则须要用__block关键字修饰。

四、关于引用计数

在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;

一、在ViewController1中建立一个按钮,跳转ViewController2

二、在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];
}

三、在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];
}

这样就解决了内存问题。

4、关于block的做用域

应避免将花括号中的block用于外面,若是须要,你能够将这个block声明为全局的。

专一技术,热爱生活,交流技术,也作朋友。

——珲少 QQ群:203317592

相关文章
相关标签/搜索