开发者

Dynamically create arrays at runtime and add to another array

开发者 https://www.devze.com 2023-03-26 23:22 出处:网络
I am working on a small isometric engine for my next iPhone game. To store the map cells or tiles I need a 2 dimensionel array. Right now I am faking it with a 开发者_Python百科1-dimensionel, and it i

I am working on a small isometric engine for my next iPhone game. To store the map cells or tiles I need a 2 dimensionel array. Right now I am faking it with a 开发者_Python百科1-dimensionel, and it is not good enough anymore.

So from what I have found out looking around the net is that in objective-c I need to make an array of arrays. So here is my question: How do I dynamicly create arrays at runtime based on how many map-rows I need?

The first array is easy enough:

NSMutableArray *OuterArray = [NSMutableArray arrayWithCapacity:mapSize];

now I have the first array that should contain an array for each row needed. Problem is, it can be 10 but it can also be 200 or even more. So I dont want to manually create each array and then add it. I am thinking there must be a way to create all these arrays at runtime based on input, such as the chosen mapsize.

Hope you can help me Thanks in advance Peter


I think this previous question should help.

2d arrays in objective c

Nothing to do with me. I have never owned an iphone or tried to write code for one.


The whole point of NSMutableArray is that you don't care. Initialize both dimensions with an approximate size and then add to them. If your array grows beyond your initial estimate the backing storage will be increased to accomodate it. This counts for both your columns (first order array), and rows (second order array).


EDIT

Not sure what you meant in your comment. But this is one way to dynamically create a 2-dimensional mutable array in Objective-C.

NSUInteger columns = 25 ; // or some random, runtime number
NSUInteger rows = 50;     // once again, some random, runtime number

NSMutableArray * outer = [NSMutableArray arrayWithCapacity: rows];

for(NSUInteger i; i < rows; i++) {
    NSMutableArray * inner = [NSMutableArray arrayWithCapacity: columns];
    [outer addObject: inner];
}

// Do something with outer array here


NSMutableArray can hold as many elements as you want to add to it. (Based on available heap though).

All you have to do is when you want to add an element(Array) to this mutable array you can add it using the addObject method.

So you create a MutableArray as follows:

NSMutabaleArray *outerArray = [NSMutableArray array]; // initially contains 0 elements.

[outerArray addobject:<anotherArray>];


From your rejection of the other answers I think you don't know how to add them in a loop, or am I wrong?

Try:

for (i = 0; i < mapSize; i++) 
    [outerArray addObject: [[NSMutableArray alloc] init]];

or, if you know or can estimate the size of the second dimension:

for (i = 0; i < mapSize; i++) 
    [outerArray addObject: [[NSMutableArray alloc] 
                            initWithCapacity: your_2nd_d_size]];

Now, how you fill the arrays, i.e. where you get the contents depends on you. In one or more loops, you do:

[(NSMutableArray *)[outerArray objectAtIndex: i] 
    addObject: your_current_object]; 
0

精彩评论

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