block的循环引用

1、创建block用copy修饰,拷贝到堆上

2、之前一直用这种写法,但会碰到提前释放的状态

__weak typeof(self)wakeself = self;

3、就用弱指针指向self,在block内部对weakSelf产生一个强引用,就解决了提前释放的问题

@weakify(self);

@strongify(self);

例如:

__weak __typeof(self) weakSelf = self;
self.block = ^{
    __strong __typeof(weakSelf) strongSelf = weakSelf;
    [strongSelf doSomething1];
    [strongSelf doSomething2];     
}

或者

#define WEAKSELF __weak typeof(self)weakSelf =self

#define StrongSelf __strong typeof(weakSelf) strongSelf = weakSelf

使用:

 WEAKSELF;

        [_cell setBurstBlock:^(NSInteger leftTag) {

            StrongSelf;

            [strongSelf showNavigationView:[GoodsViewController new]];

        }];

 

 

 

posted @ 2017-04-24 16:25  柳仙慧子  阅读(143)  评论(0编辑  收藏  举报