Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With an NSArray of object references, do I explicitly release all objects in the array or just the array itself?

My class has an NSArray that is filled with objects. In my dealloc method, can I simply call release on my NSArray, or do I need to iterate the array and release all objects first?

like image 722
Rich Avatar asked May 20 '09 01:05

Rich


People also ask

How do you declare NSArray in Objective C?

In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method. id objects[] = { someObject, @"Hello, World!", @42 }; NSUInteger count = sizeof(objects) / sizeof(id); NSArray *array = [NSArray arrayWithObjects:objects count:count];

Can NSArray contain nil?

arrays can't contain nil.

What is NSArray?

NSArray is an immutable Objective C class, therefore it is a reference type in Swift and it is bridged to Array<AnyObject> . NSMutableArray is the mutable subclass of NSArray .


2 Answers

You can call release directly on the NSArray. The implementation of NSArray will take care of sending release to all the objects stored in the array.

like image 108
Marc W Avatar answered Sep 28 '22 03:09

Marc W


NSArray retains objects when they're added, and releases them when they're removed or the array is deallocated. Keep this in mind, it's this concept of "ownership" that retain/release memory management is built upon. It's the same with the object that owns the array, if it also retained the objects in the array you will need to send them another release message in your dealloc implementation. If not, and if no other objects retained them, they'll be deallocated once the array releases them.

like image 25
Marc Charbonneau Avatar answered Sep 28 '22 02:09

Marc Charbonneau