ios那些事儿

block中成员变量和属性的区别

2017-07-28  本文已影响21人  4VZhang
.h文件
#import <Foundation/Foundation.h>
@interface person : NSObject
/** block */
@property (nonatomic, copy) void(^ageBlock)(int age);
@end

.m文件
#import "person.h"
@implementation person

@end
.m文件
#import "person.h"
#import "testViewController.h"

@interface testViewController ()
/** person */
@property (nonatomic, strong) person *p;
/** value */
@property (nonatomic, assign) int value;

@end

@implementation testViewController {
    int _value;
}

属性

1.在生成对应的成员变量_value的同时;
2.也会自动生成setter/getter方法.
使用self.value实际在调用两个方法来实现对变量的操作.

测试代码如下:

-(void)dealloc {
    NSLog(@"释放当前vc对象");  
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.p = [[person alloc] init];
    
    __weak typeof(self) weakSelf = self;
    self.p.ageBlock = ^(int age) {
        
        //1.1 成员变量写法(自动报警,提示会产生循环引用)
        _value = 50;
        NSLog(@"_value-----%d",_value);
        
        // 1.2等价成员变量(自动报警,提示会产生循环引用)
        self->_value = 30;
        NSLog(@"self->_value---%d",self->_value);
    
        // 1.3成员变量弱引用(报错)
        //weakSelf->_value = 20;
        //NSLog(@"weakSelf->_value----%d",weakSelf->_value);
        
        // 1.4成员变量weak-strong dance
        __strong typeof(self) strongSelf = weakSelf;
        strongSelf->_value = 10;
        NSLog(@"strongSelf->_value--%d",strongSelf->_value);
        
        
        //2.1属性未弱引用写法(自动报警,提示会产生循环引用)
        
        self.value = 50;
        NSLog(@"%d",self.value);
        
        //2.3 属性的写法(由成员变量可以类推到也会有风险,推荐使用weak-strong dance方式)
        weakSelf.value = age;
        NSLog(@"%d",weakSelf.value);
        
        //2.3 属性的写法(weak-strong dance)
        __strong typeof(self) strongSelf = weakSelf;
        strongSelf.value = age;
        
        NSLog(@"%d",strongSelf.value);
    };
}

    self.p.ageBlock(10);

几种写法最终测试结果如下

1.1 没有释放当前vc,说明当前vc继续被强引用,没有被销毁
2017-07-28 15:02:25.114 test_Property[20484:1869126] _value-----10
2017-07-28 15:02:26.325 test_Property[20484:1869126] -[testViewController viewWillDisappear:],控制器pop出去

1.2 没有释放当前vc,说明当前vc继续被强引用,没有被销毁
2017-07-28 15:13:12.727 test_Property[20539:1878132] self->_value---10
2017-07-28 15:13:14.764 test_Property[20539:1878132] -[testViewController viewWillDisappear:],控制器pop出去

1.3 见下面 xcode自测报错图

1.4 释放当前vc,并被销毁
2017-07-28 15:17:27.160 test_Property[20568:1883004] strongSelf->_value--10
2017-07-28 15:17:28.627 test_Property[20568:1883004] -[testViewController viewWillDisappear:],控制器pop出去
2017-07-28 15:17:29.131 test_Property[20568:1883004] 释放当前vc对象

2.1 没有释放当前vc,说明当前vc继续被强引用,没有被销毁
2017-07-28 15:19:04.250 test_Property[20583:1884117] 10
2017-07-28 15:19:05.301 test_Property[20583:1884117] -[testViewController viewWillDisappear:],控制器pop出去

2.2 释放当前vc,并被销毁
2017-07-28 15:21:50.294 test_Property[20621:1886605] 10
2017-07-28 15:21:51.488 test_Property[20621:1886605] -[testViewController viewWillDisappear:],控制器pop出去
2017-07-28 15:21:51.994 test_Property[20621:1886605] 释放当前vc对象

2.3 释放当前vc,并被销毁
2017-07-28 15:22:52.713 test_Property[20637:1887821] 10
2017-07-28 15:22:54.250 test_Property[20637:1887821] -[testViewController viewWillDisappear:],控制器pop出去
2017-07-28 15:22:54.756 test_Property[20637:1887821] 释放当前vc对象
xcode自测警告图.png

Capturing 'self' strongly in this block is likely to lead to a retain cycle

xcode自测报错图.png

Dereferencing a __weak pointer is not allowed due to possible null value caused by race condition, assign it to strong variable first

简单翻译一个SO的答案:Why would you use an ivar?,展开说一下成员变量的好处。

注意:
在block中最好使用属性,并使用weak-strong dance弱引用
使用成员变量(_value)常常忽略掉前面的self指针,容易产生循环引用的,泄露内存;

参考文章:
属性(property)与成员变量(ivar)有什么区别
iOS中self.和下划线的区别

上一篇 下一篇

猜你喜欢

热点阅读