I am new to iphone development . I am using ARC for my project. As far as I understood using ARC we don’t have to release any object manually. But , I have observed in some places , people explicitly set their object to nil in the ViewDidUnload even after using ARC.
For example, in .h file I have something like this:
@property (unsafe_unretained, nonatomic) IBOutlet MKMapView *mapViewOutlet;
@property (unsafe_unretained, nonatomic) IBOutlet UIToolbar *toolBar;
@property (strong,nonatomic) NSMutableArray *dataArray;
And .m as follows:
- (void)viewDidUnload
{
[self setMapViewOutlet:nil];
[self setToolBar:nil];
[super viewDidUnload];
self.dataArray=nil;
}
My question is, is it really necessary to explicitly specify nil in the ViewDidUnload even under ARC?
The whole point of the
viewDidUnloadmethod is to release data that you don’t really need, in order to free memory. Read the documentation:So you’re setting the properties to
nilin order to release the objects now and help the system to free up some memory. But of course this depends on the property type – strong properties are “yours” and only you can decide whether to release them now (by setting tonil) or not. Weak properties could already benil, for example if they pointed to some views that got released with the main view. Andunsafe_unretainedproperties are a special beast. The object they point to might already been released, but that does not mean they were set tonilautomatically. So you should either use one of the “safer” property types (strong/weak), or set the unsafe properties tonilhere, to make sure you won’t use the released object later. There are no hard rules in this case, you have to think about the situation and what it means for the various properties.By the way,
viewDidUnloadis getting deprecated in iOS 6, where no views are being released under low-memory conditions anymore. You still receive thedidReceiveMemoryWarningcallback, so that you can release some resources there if you want to. Again, I suggest that you read the documentation and run a few tests to see what happens and decide what you should do.