25

How do I declare a two dimensional array of string type in Objective-C?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ashish
  • 787
  • 3
  • 12
  • 18
  • If you're talking NS(Mutable)Array, you don't "declare" it. An NSArray has no assigned content type, so you just declare the outer array. How you use it is up to you. – Hot Licks Jul 29 '13 at 11:05

3 Answers3

44

First, you might consider using a class to hold your inner array's strings, or loading it from a plist file (in which it is easy to make an 2d array of strings).

For direct declarations, you have a couple of options. If you want to use an NSArray, you'll have to manually create the structure like this:

NSMutableArray *strings = [NSMutableArray array];
for(int i = 0; i < DESIRED_MAJOR_SIZE; i++)
{
    [strings addObject: [NSMutableArray arrayWithObject:@"" count:DESIRED_MINOR_SIZE]];
}

Or, using array literals, you can get an immutable version like this:

NSArray *strings = @[ @[ @"A", @"B", @"C" ], @[ @"D", @"E", @"F" ], @[ @"G", @"H", @"I" ] ]

You can then use it like this:

NSString *s = [[strings objectAtIndex:i] objectAtIndex:j];

This is somewhat awkward to initialize, but it is the way to go if you want to use the NSArray methods.

An alternative is to use C arrays:

NSString *strings[MAJOR_SIZE][MINOR_SIZE] = {0}; // all start as nil

And then use it like this:

NSString *s = strings[i][j];

This is less awkward, but you have to be careful to retain/copy and release values as you put them in to and remove them from the array. (Unless you're using ARC, of course!) NSArray would do this for you but with C-style arrays, you need to do something like this to replace an array:

[strings[i][j] release];
strings[i][j] = [newString retain];

The other difference is that you can put nil in the C-style array, but not the NSArrays - you need to use NSNull for that. Also take a look at Stack Overflow question Cocoa: Memory management with NSString for more about NSString memory management.

Community
  • 1
  • 1
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
12

If you want to declare and initialize a two-dimensional array of strings, you can do this:

NSArray *myArray = [NSArray arrayWithObjects:
                       [NSArray arrayWithObjects:@"item 1-1", @"item 1-2", nil],
                       [NSArray arrayWithObjects:@"item 2-1", @"item 2-2", nil],
                       [NSArray arrayWithObjects:@"item 3-1", @"item 3-2", nil],
                       [NSArray arrayWithObjects:@"item 4-1", @"item 4-2", nil],
                       nil];

This has the benefit of giving you an immutable array.

ellockie
  • 3,730
  • 6
  • 42
  • 44
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
2

I might be self-advertising but I wrote a wrapper over NSMutableArray fore easy use as a 2D array. It available on GitHub as CRL2DArray here. https://github.com/tGilani/CRL2DArray

atastrophic
  • 3,153
  • 3
  • 31
  • 50
  • Coming from Android (Java) I am surprised how difficult it is to create a 2D Array in obj-C, thanks for the very useful GitHub sir! – Lazar Kukolj Aug 23 '16 at 19:18
  • The only problem is that I wish there was more built-in methods that a normal NSArray has like count, etc. But for now I'll just manually do it! – Lazar Kukolj Aug 23 '16 at 20:00