在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()
}

 

相关文章:

  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-14
  • 2021-07-14
  • 2022-12-23
  • 2022-02-26
相关资源
相似解决方案