1

I am having a bit of trouble creating a dynamically allocated two-dimensional array. If it makes a difference I am coding for ios.

I am a bit new to objective c and the memory allocation in this language is a bit confusing. I did a bit of research and come up with NSMutableArray and this link, 2D arrays using NSMutableArray, but am still confused.

What I am looking to do is create a list of rows and colunms, each row containing a dynamically created number of column objects. The number of rows is also dynamic.

If you think of the layout as a a hash map; graphically, each hash cell will contain a button and a label displayed on the screen.

EDIT:

I have,

NSMutableArray *rows;
NSMutableArray *columns;

- (void)viewDidLoad {
    rows = [[NSMutableArray alloc] init];
    columns = [[NSMutableArray alloc] init];

    //allocate memory?
}

My main question is how to allocate the memory for each entry?

EDIT2:

If anyone needed help with this I got the answer and look more to dictionaries instead of Arrays, they have lots of helpful methods.

1

2 Answers 2

4

You cannot create 2 dimensional array with objective-c but you can use c style array, for example:

@implementation TwoDimCArray
{
    NSUInteger _array[8][8];
}

- (id)init
{
    if (self = [super init]){
        [self clearArray];
    }
    return self;
}

- (int)cellStateAtColumn:(NSInteger)column andRow:(NSInteger)row
{
    return _array[column][row];
}

- (void)setCellState:(BoardCellState)state forColumn:(NSInteger)column andRow:(NSInteger)row
{
    _array[column][row] = state;
}

- (void) clearArray
{
    memset(_array, 0, sizeof(NSUInteger) * 8 * 8);
}
@end

It works very quick when you compare it with NSArray embedded in another NSArray. Hope it help.

Sign up to request clarification or add additional context in comments.

7 Comments

Will this work with strings as well? And how do I reallocate the memory after creating the array? It may be there again I am new to objective c.
Sorry, what do you mean reallocate the memory? Do you want do destroy the array and create another one or do you want to copy this array? Can you give some example what are you trying to do? You can use it like objective-c object: S TwoDimCArray* array = [[TwoDimCArray alloc] init];
Basically increase the size. I am reading a list of objects from an xml file. Each object has 2 identifiers; group, and sub_group (each object is its own subgroup, or column). I parse the info from the document and gather the group and sub_group from each object. What this array will hold is a string reference to each respective object. So when I run into an object I will loop through the array and if this object belongs to a group that is already initialized I will add a subgroup index containing a string of this object. And if there is no group that matches this one add a group.
Google it there is plenty of tutorial chow to resize c array.
Since when can't you create 2 dimensional arrays in objective-c? Please back this statement up with evidence. NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil], [NSMutableArray arrayWithObjects:@"4", @"5", @"6", nil]]; then you could do ` array[1][2]` which would return 6. Boom 2 dimensional array.
|
2

So you need to create one array which will hold all of the rows. Then, for each row you need to create a new array to hold the data for that row, the number of elements in this array is the number of columns this row has.

Does that make sense? You essentially end up with an array of arrays.

2 Comments

It does make sense. I am mainly lost on how to 'push' a new row when I need it, and how to 'push' a new column object for each row. If it helps I plan to have a URL string stored in each column object.
To do this you need to use NSMutableArray objects. Normal arrays (NSArray) cannot have objects added to them once they are created. So lets assume you have created a row array like this: NSMutableArray *newRow = [[NSMutableArray alloc] init]; you add a new element (column data) into it by calling [newRow addObject:ColumnObjectToAdd];. If you provided some example data we could show you a worked example if that would help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.