IOS 避免self循环引用的方法的实例详解 示例代码: // - weak strong #define myWeakify(VAR) \ try {} @finally {} \ __weak __typeof__(VAR) VAR##_myWeak_ = (VAR) #define myStrongify(VAR) \ try {} @finally {} \ __strong __typeof
IOS 避免self循环引用的方法的实例详解
示例代码:
// - weak & strong #define myWeakify(VAR) \ try {} @finally {} \ __weak __typeof__(VAR) VAR##_myWeak_ = (VAR) #define myStrongify(VAR) \ try {} @finally {} \ __strong __typeof__(VAR) VAR = VAR##_myWeak_ #define myStrongifyRetVIfNil(VAR, RET) \ try {} @finally {} \ __strong __typeof__(VAR) VAR = VAR##_myWeak_;\ if(VAR == nil) return RET #define myStrongifyRetNIfNil(VAR) \ try {} @finally {} \ __strong __typeof__(VAR) VAR = VAR##_myWeak_;\ if(VAR == nil) return
使用如下:
- (void)handleFromVC { NSArray *viewControllers = self.navigationController.viewControllers; @myWeakify(self); [viewControllers enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { @myStrongifyRetNIfNil(self); //代码 }]; }
如有疑问请留言或到本站社区交流讨论,感谢阅读,希望通过本文能帮助到大家,谢谢大家对本站的支持!