0

How do i produce a 2 dimensional NSMutable array as this:

Array:

=>[item1]=>[item1a,item1b,item1c...]
=>[item2]=>[item2a,item2b,item2c...]
...
=>[item10]=>[item10a,item10b,item10c...]

So far i've only been successful up to the [item1]=>[item1a,item1b,item1c...]

When i try to add more 2 dimensional array it keeps overriding the first row.

7
  • Stay tuned, some of the additions made to clang will allow some shorthand syntax for creating arrays that will make multi-dimensional arrays much easier. Syntax will look like array = @[ @[ item1a, item1b ], @[ item2a, item2b ] ];. This will probably be part of the next release of Xcode. Commented May 10, 2012 at 4:36
  • 1
    Just how many times will this question be asked? stackoverflow.com/questions/10485521/… Commented May 10, 2012 at 4:37
  • @Richard: The question you linked is not about NSArrays inside of NSArrays. Commented May 10, 2012 at 18:03
  • @JacquesCousteau it's about 2 dimensional arrays in objective-c. The answer can be used for objects as well. Commented May 10, 2012 at 18:35
  • @RichardJ.RossIII: Except there is no clear ownership of objects. Commented May 10, 2012 at 20:21

4 Answers 4

5

Create NSMutableArray and assign NSMutableArrays to it as its objects.

For example:

NSMutableArray * myBig2dArray = [[NSMutableArray alloc] init];

// first internal array
NSMutableArray * internalElement = [[[NSMutableArray alloc] init] autorelease];
[internalElement addObject:@"First - First"];
[internalElement addObject:@"First - Second"];
[myBig2dArray addObject:internalElement];

// second internal array
internalElement = [[[NSMutableArray alloc] init] autorelease];
[internalElement addObject:@"Second - First"];
[internalElement addObject:@"Second - Second"];
[myBig2dArray addObject:internalElement];
Sign up to request clarification or add additional context in comments.

Comments

3

To make a 2 dimensional array you would make an array of arrays.

NSArray *2darray = [NSArray arrayWithObjects: [NSArray arrayWithObjects: @"one", @"two", nil], NSArray arrayWithObjects: @"one_2", @"two_2", nil]];

It gets very verbose but that is the way I know how to do this. An array of dictionaries may be better for your situation depending on what you need.

Comments

0

I wrote an NSMutableArray wrapper for easy use as a Two Dimensional array. It is available on github as CRL2DArray here . https://github.com/tGilani/CRL2DArray

1 Comment

This would be a lot more useful if it provided a way to access the two dimensional array in a 1 dimensional way. Example: array{ {orange, pear} {red, blue, green} {square, circle} } .. array1D[3] = blue
0

First you to have set An NSMutableDictionary on .h file

        @interface MSRCommonLogic : NSObject
        {
            NSMutableDictionary *twoDimensionArray;
        }

        then have to use following functions in .m file


        - (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
        {
            if(!twoDimensionArray)
            {
                twoDimensionArray =[[NSMutableDictionary alloc]init];
            }

            NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
            [twoDimensionArray setObject:value forKey:strKey];

        }

        - (id)getValueFromArray :(int)rows cols:(int) col
        {
            NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
            return  [twoDimensionArray valueForKey:strKey];
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.