Detailed example of IOS method to avoid self circular reference

Time:2021-11-23

Detailed example of IOS method to avoid self circular reference

Example code:


// - 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 

Use the following:

- (void)handleFromVC 
{ 
  NSArray *viewControllers = self.navigationController.viewControllers; 
  @myWeakify(self); 
  [viewControllers enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 
    @myStrongifyRetNIfNil(self); 
    //Code 
  }]; 
}

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope this article can help you. Thank you for your support to this site!

Recommended Today

Application of observer pattern in design pattern in Ruby Programming

Observer mode(sometimes referred to as publish / subscribe mode) is softwareDesign patternA kind of.In this mode, a target object manages all its dependent observer objects and actively notifies when its own state changes.This is usually achieved by calling the methods provided by each observer. When implementing the observer mode, it should be noted that the […]