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?
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];
arrays can't contain nil.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With