Creating objects in a for loop

Nibbie
Posts: 4
Joined: 2009.07
Post: #1
Hi,

I'm hoping someone might be able to help me with this. Spend all last night puzzling over this one and not getting anywhere. I'm very much a beginner when it comes to programming.

Anyhow what I'd like to do is create a certain number of objects in a for loop:

Code:
for (int i = 0; i < width; i++)
{
    NSMutableArray * row = [NSMutableArray alloc];
}

What I'm struggling with is using the i variable to create the name of each object. e.g. row1, row2, row3. I'm not sure what the correct syntax is. I did try row[i] but i in that case just refers to how big you want the array.

Thanks for any help.
Quote this message in a reply
Member
Posts: 24
Joined: 2008.02
Post: #2
First, you need to init your NSMutableArray before you can use it, like so:

Code:
NSMutableArray * row = [[NSMutableArray alloc] initWithCapacity:someNumber];

Secondly, after your for loop is done, you won't be able to use any of the objects you created in it. To keep them around, you can add them to an existing array and access them by index. If you do this, the array will retain the object. The NSMutableArray class has a method that will create an array for you that is auto released, that is, it is released after the current method is done running. The reason why we want the object to be auto released is that the row array will retain the object for us. If we don't auto release the object, it will stay in memory even when the row array is released, risking a memory leak. Here is what it looks like.

Code:
NSMutableArray *rowArray = [[NSMutableArray alloc] initWithCapacity:width];

//NSInteger is just an int
NSInteger i;
for (i=0; i<width; i++) {

//Add the object to the row array. Do whatever else you want to the object.
[rowArray addObject:[NSMutableArray arrayWithCapacity:someNumber]];
}

Can I ask what you're trying to do? There may be another way to do it.
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.07
Post: #3
Thanks so much for your reply.

I should have explained in full what I'm trying to achieve. I'm having a stab at making in isometric game for the iPhone using the cocos2d engine.

What I am working on is a class that will draw a grid of a certain size.

The idea I was going with was to use a for loop to create a MutableArray for each row on the grid and add it into one MutableArray. Similar to a multidimensional array but in this case I would like to add objects.

For the second for loop I then intend to add a certain number of Sprite objects, one for each tile in a row.

Then finally I want to go through the array and add all the objects into a layer.

Beyond that I'm going to read up some more and include some input so I can move around the grid and have it cover an area bigger than the size of the screen.

The code below is incomplete as I was stuck on the creating arrays part. It also includes some old test code where I was just testing to see if I could draw a couple of sprites onto the screen.

Code:
@implementation DrawGrid
- (id) getWidth:(int)width getLength:(int)length;
{
    NSMutableArray * terrainArray = [NSMutableArray alloc];
    for (int i = 0; i < width; i++)
    {
        NSMutableArray * row = [NSMutableArray alloc];
        [terrainArray addObject:row];
    }
    for (int i = 0; i < length; i++)
    {
        //Add sprite tiles into each array    
    }
    Layer * terrainLayer = [DrawGrid node];
        //Add loop to add contained sprites into Layer

        //Old test code. Please ignore.
    Sprite * tile1 = [[DrawTile alloc]setX:240 setY:160];
    Sprite * tile2 = [[DrawTile alloc]setX:272 setY:144];
    [terrainLayer addChild:tile1 z:0];
    [terrainLayer addChild:tile2 z:1];
    return terrainLayer;
}
@end
Quote this message in a reply
Moderator
Posts: 623
Joined: 2007.09
Post: #4
Hmm, not entirely understanding your post, but it looks like you want an array of sprites inside an array of rows:

TerrainArray - RowArray RowArray RowArray - Sprite Sprite Sprite

If that's the case, then you can use nested for loops.

Code:
- (void)myCoolFunctionWithRows:(int)rows columns:(int)columns{
     NSMutableArray *terrainArray = [[NSMutableArray alloc] init]; //create the top level array(which will hold arrays of sprites)
     int a;
     int b;
     for(i = 0; i < rows; i++){
          NSMutableArray *row = [[NSMutableArray alloc] init]; //create the second level array that will hold the actual sprites
          for(b = 0; b < columns; b++){
               Sprite *myCoolSprite = [[Sprite alloc] init]; //put your sprite initialization code here...
               [row addObject:myCoolSprite]; //and add the sprite to the row array
          }
          [terrainArray addObject:row];
     }

}

- Lincoln Green
http://www.binkworks.com/
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.07
Post: #5
Ahh. Thanks. I think the confusion was because I thought I had to give each array its own unique name but of course it doesn't matter as its getting copied into the main array.

Thanks for all your help.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #6
Note that Hairball's example leaks memory unless it is garbage collected.
Quote this message in a reply
Moderator
Posts: 623
Joined: 2007.09
Post: #7
Josh Wrote:Note that Hairball's example leaks memory unless it is garbage collected.

Sorry, bad assumption to make...

Code:
- (void)myCoolFunctionWithRows:(int)rows columns:(int)columns{
     NSMutableArray *terrainArray = [[NSMutableArray alloc] init]; //create the top level array(which will hold arrays of sprites)
     int a;
     int b;
     for(i = 0; i < rows; i++){
          NSMutableArray *row = [[NSMutableArray alloc] init]; //create the second level array that will hold the actual sprites
          for(b = 0; b < columns; b++){
               Sprite *myCoolSprite = [[Sprite alloc] init]; //put your sprite initialization code here...
               [row addObject:myCoolSprite]; //and add the sprite to the row array
               [sprite release];
          }
          [terrainArray addObject:row];
          [row release];
     }
}

- Lincoln Green
http://www.binkworks.com/
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.07
Post: #8
Just tried out the code last night. Works fine. Thank you.
Quote this message in a reply
Post Reply