Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under ARC, need to set nil to all the strong member when unload the viewcontroller? Is that a must?

Suppose I have a view controller like this:

@interface ControllerA : viewcontroller{
    NSString * __strong a;
}

@end

and in viewDidLoad function I set

a = [[NSSString alloc] init];

And in another ControllerB,

{
    ControllerA * controllerA = [[ControllerA alloc] init];
}

Will controllerA's member be released?

like image 743
jeswang Avatar asked Jul 12 '12 04:07

jeswang


2 Answers

Yes, the string pointed to by a will be released when controllerA is deallocated. You don't need to set it to nil yourself.

Transitioning to ARC Release Notes is currently the place to look for more information about working with ARC. One important point that it makes is that you may still need a custom -dealloc if your class needs to do anything other than releasing instance variables when it's instances are deallocated.

like image 180
Caleb Avatar answered Sep 22 '22 19:09

Caleb


You never need to set a variable to nil in ARC unless you want to ensure its deallocation as soon as possible (i.e. [myObject release] under non-ARC code). All iVars will be automatically released when the object is deallocated.

like image 30
borrrden Avatar answered Sep 21 '22 19:09

borrrden