Does automatic reference counting compute an object if I set the pointer to zero?

Does automatic reference counting free an object if I set a pointer to zero or assign a pointer to another object?

For example, do something like this:

//in .h file @interface CustomView : UIView { UIView *currentView; } // in .m file: -(void)createView1 { currentView = [[UIView alloc] init]; [self addSubview:currentView]; } -(void)createView2 { [currentView removeFromSuperview]; // does the former view get released by arc // or does this leak? currentView = [[UIView alloc] init]; [self addSubview:currentView]; } 

If this code is leaking, how would I correctly declare * currentView? Or how can I make ARC a β€œrelease” of currentView? thanks!

+4
source share
2 answers

With ARC, you do not need to think about release/retain .

Since your variable will be implicitly defined as strong , there is no need to set it to NULL - it will be released before it is assigned.

Personally, although I prefer to declare properties:

 @property (strong, nonatomic) UIView *currentView; 
+6
source

After executing [currentView removeFromSuperview] you should call currentView = nil , and ARC will do this by releasing the magic. You can then reassign currentView this new UIView.

+3
source

Source: https://habr.com/ru/post/1383296/


All Articles