2
votes

My project is ARC enabled. I have a class which is direct subclass of NSObject and my class has an NSArray(Strong reference). How can I release my array?

My understanding

  1. In ARC no need of releasing objects

  2. set nil to my NSArray(Strong reference) in dealloc method

  3. set nil to my NSArray(Strong reference) in viewDidUnload incase of view controller

This is the correct way of releasing my NSArray? If It's not then what is the correct approach?

1

1 Answers

5
votes

So first of all, viewDidUnload is no longer used so that is not an option. Second of all, you don't need to manually clear out instance variables of a class when it is deallocated unless they require some special cleanup. When an object is deallocated, it releases ownership of all of its instance variables so they will be deallocated automatically as long as nothing else is pointing to them with a strong reference. So the correct approach, as you asked, is to do nothing. The array will be deallocated on its own once your object is deallocated.

That said, if you really want to, you can clear out the pointer to the array like this:

myArray = nil;

This will then deallocate the array if there are no other strong references pointing to it which will in turn deallocate any elements in the array that have no other strong references pointing to them.