I have a “Note” class, and I have a Tuner Class. My Tuner class is initializing an NSArray* in it’s init function.
I want to initialize this array to hold 88 notes, and right now I am creating things like Note* key1; etc etc.
I am just making sure that I need to alloc things for these notes so that they do not go out of scope once init finishes, and my NSArray stays in tact with its entries, or am I able to just declare Note key1 and insert into the array and it is taken care of for me.
An example piece of code creating an NSArray containing a few objects such that the array will stay around even after the function exits would be very helpful. Thank you!
NSArrayandNSMutableArraysend-retainto objects when they are added, so the appropriate thing to do is to-releasethe objects you add explicitly or use autoreleased objects.Here’s a quick example for the
Tunerclass’s-initmethod:You will need to
+allocevery Note object and-releaseit, since adding it to the array retains it. To destroy all the note objects when you’re done, make sure yourTunerclass releases the array in its-deallocmethod: