开发者

Generating objects for a grid of nodes in Obj-C

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 im开发者_JS百科agine this being a wrong way to view this. If so, I would like advice on how to tackle this properly.

Thanks in advance.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜