hrtachetosing
hrtachetosing

Reputation: 13

iOS Object Array: Creating an array of objects inside a function

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!

Upvotes: 1

Views: 15841

Answers (4)

Jacob M. Barnard
Jacob M. Barnard

Reputation: 1367

Inside the class which will hold the notes array.

  1. Create a property using (nonatomic, retain) of type NSMutableArray called notes.
  2. Synthesize it.
  3. Make sure it is released in the dealloc method.

Inside the method for creating the array:

  1. init and alloc a new temporary NSMutableArray.
  2. Loop and add all Note objects to the array.
  3. Adding this array to notes.

Upvotes: 1

more tension
more tension

Reputation: 3352

NSArray and NSMutableArray send -retain to objects when they are added, so the appropriate thing to do is to -release the objects you add explicitly or use autoreleased objects.

Here's a quick example for the Tuner class's -init method:

- ( id )init
{
    NSInteger    i;
    Note         *note;

    self = [ super init ];
    if ( self ) {
        notes = [[ NSMutableArray alloc ] initWithCapacity: 88 ];
        for ( i = 0; i < 88; i++ ) {
            note = [[ Note alloc ] initWithKey: i ];
            [ notes addObject: note ];
            [ note release ];
        }
    }

    return( self );
}

You will need to +alloc every Note object and -release it, since adding it to the array retains it. To destroy all the note objects when you're done, make sure your Tuner class releases the array in its -dealloc method:

- ( void )dealloc
{
    [ notes release ]; // this sends release to all objects in the array, too

    [ super dealloc ];
}

Upvotes: 2

Dan F
Dan F

Reputation: 17732

Here's a simple example of how you might do that in the init function of your Tuner class

for ( int i = 0; i < 88; ++i )
{
    Note *key = [[Note alloc] init];
    [myNotes addObject:key];
    [key release];
}

Please note that in my example, myNotes is assumed to be of the type NSMutableArray It is the only way you can construct an array by adding objects individually like that.

Upvotes: 2

Ryan Gant
Ryan Gant

Reputation: 23

Well, I'm not totally sure what you are trying to do, but you could just make an NSMutableArray as a global variable. You could then add and remove any object of any type to this array. The array would still be in memory until you call it's release method.

Upvotes: 0

Related Questions