在block中常常会用到self,可是会造成循环引用。这时候就需要这样来解决这个问题:
#define WeakSelf __weak typeof(self) weakSelf = self
#define StrongSelf __strong typeof(weakSelf) self = weakSelf
//OC
- (void)aFunc:(id)sender { WeakSelf;//
[UIView animateWithDuration: animations:^{
StrongSelf;//
[self.view doSomething];//use self
}];
}
//swift 2.2
self.closure = { [unowned self] in
self.doSomething()
}