views:

41

answers:

1

Hi there,

For a program I am developing I have to create a grid of nodes. For instance a grid of 10x10 = 100 nodes. A node can contain several variables. I am having problems visualizing this in a Object Oriented way. My idea is that I let the program automatically create an object for every node, as I have made a class "Node". My question is: Is it possible to let Objective-C (2.0) create automatically a large number of objects (for instance node1 to node100)?

As I am quite new to programming I can imagine this being a wrong way to view this. If so, I would like advice on how to tackle this properly.

Thanks in advance.

+1  A: 

It sounds like you're headed in the right direction. There a lot of ways you could store the grid, I'll show you a 2D C array. You can't init multiple objects in one method call, but it's simple to create a for loop to do it.

Node * nodes[10][10];
for (int x = 0; x < 10; x++)
    for (int y = 0; y < 10; y++)
        nodes[x][y] = [[Node alloc] init];

This is just one simple way of doing it, but I think it demonstrates the general idea.

Cory Kilger
This worked really well, thanks so much!
Job