views:

71

answers:

5

I'm a beginner in Objective-C and I'm trying to find the most convenient way to work with multidimensional arrays in Objective-C. Either I am missing something or they are very ugly to work with.

Let's say we have a classic problem:

  • read input from file; on the first line, separated by space(" ") are the width and height of the matrix (eg: 3 4)
  • on the following lines there is the content described by the values above

Eg:

3 4
a b c d
e f g h
i j k l

The first solution I thought of was:

NSMutableArray *matrix = [[NSMutableArray alloc] initWithCapacity: x]; //x = 3 in this specific case
NSMutableArray *cell;
for(cell in matrix)
{
    cell = [NSMutableArray initWithCapacity: y];
    for(int i = 0; i < y; i++) // y = 4
    {
        //object is a NSString containing the char[i][j] read from the file
        [cell insertObject:object atIndex: i];
    }
}

This was the first thing I had in mind when thinking about how I should get my values read from file in a multidimensional array. I know you can use C arrays, but since I will store NSObjects in it, I don't think is such a great idea. Nonetheless, from my point of view is easy to work with C arrays rather the solution I got with Objective-C.

Is there another way you could build a multidimensional array in obj-c and easier than the one above?

How about looping them? I know I can do something like

NSArray *myArray;
for(int i=0; i < [array count]; i++)
{
    [myArray arrayWithArray: [array objectAtIndex: i]];
    for(int j=0; j < [myArray count]; j++)
    {
        NSLog(@"array cell [%d,%d]: %s", i, i, [myArray objectAtIndex: j]);
    }
}

But that is still more complicated than your average C multidimensional array loop.

Any thoughts on this?

+1  A: 

Objective-C is a superset of C, if you want to work with multidimensional arrays like you would in C, do it that way. If you want to work with objects doing it the Cocoa way, then that's fine too, but you will write more code to do it.

jer
I know Objective-C is a superset of C, but I'd like to work with NSArray/NSMutableArray in a simpler way than the one described in my question (if that is possible, of course)
Bogdan Constantinescu
Well your code has some problems, you'll want to read up on the rules for memory management, and learn to pass class methods to classes, and instance methods to instances (first block of code, inside your outer for loop for instance). But yeah, you'll do something very similar to this to do what you want to do with objects.
jer
A: 

Can you not simply make an array of id?

#import <Foundation/Foundation.h>

int main(int argc, char **argv) {

        id ptr[3][4];
        NSObject *p;

        ptr[0][0] = p;

}
Marc van Kempen
It would me nice if there was a solution to get it in a `NSArray`. I mean, contruct it the C way and then put it in a `NSArray` if it would be possible (something like `[NSArray arrayWithArray: ptr]`)
Bogdan Constantinescu
I'm not sure if that is possible, objective-c does not have operator overloading as far as I know.
Marc van Kempen
A: 

You can do nothing more with NSArray or NSMutableArray in this regard. That is there is nothing like

objectAtIndex:i :j

taskinoor
A: 

You can always create one-dimensional arrays of the size width * height instead:

const int size = width*height;
NSMutableArray *array = [NSMutableArray arrayWithCapacity:size];

for (int i=0; i<size; ++i) {
    NSString *string = [NSString stringWithFormat:@"col=%d, row=%d", i%width, i/width];
    [array insertObject:string atIndex:i];
}
Georg Fritzsche
A: 

Nobody uses direct multi-dimensional arrays of any size in any computer language (except for homework). They simply use too much memory and are therefore too slow. Objective-C is an object-oriented language. Build a class that does what you need.

Stephan Eggermont
Is it a crime to know what you need to put into an array before declaring it? I frankly don't think so. I like to write custom classes, but come on, we're talking about a multidimensional array and nothing more. Even C doesn't need special data structures for multidimensional arrays [the mean data structure I need in this particular case is a simple char**]
Bogdan Constantinescu
Such as a hypothetical NSMultidimensionalArray? No problem having such an object in an OO language. http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
Douglas
Your assertion that because something uses more memory it is slow is a fallacy. Take for instance, a sparse array (commonly used for dispatch tables in late bound dynamic languages like Objective-C) are actually the fastest way I can think of to implement dispatch tables, even though they (can) use a lot more memory than would a radix trie for instance.
jer
That does not have anything to do with a direct multi-dimensional array. Smalltalk (and Objective-C) use an inline cache and a hash table as far as I'm aware. They are very small anyway, as you have a table for each type, not for each instance. So no fallacy there.
Stephan Eggermont
Oh, and a sparse array is why nobody uses direct multi-dimensional arrays
Stephan Eggermont