iOS __weak __strong WeakSelf StrongSelf

 

 

在block中常常会用到self,可是会造成循环引用。这时候就需要这样来解决这个问题:

 

#define WeakSelf __weak typeof(self) weakSelf = self
#define StrongSelf __strong typeof(weakSelf) self = weakSelf

 

 

//OC
- (void)aFunc:(id)sender {
    
    WeakSelf;//1
    [UIView animateWithDuration:1 animations:^{
        StrongSelf;//2
        [self.view doSomething];//use self
    }];
}

 

 

//swift 2.2
self.closure = { [unowned self] in
    self.doSomething()
}

 

posted @ 2016-04-15 14:57  Ficow  阅读(238)  评论(0编辑  收藏  举报