2

I'm quite confused on how to add objects in multidimensional arrays.

Is initializing multidimensional arrays are the same with just a simple array?

This is my initialization.

testList = [[NSMutableArray alloc] init];

I need to do something like

testList[i][j] = item;

i tried

[[[testList objectAtIndex:i]objectAtIndex:j] addObject:item];

but it doesn't seem to work :(

1
  • you should add more NSMutableArray (or NSArray) objects to your original testList, because there is no multi dimension array in objective-c; you could use the NSMutableDictionary (or NSDictionary) with KVC; or you could combined them with each other; or you could use NSMutableSet (or NSSet) or you could combine everything with everything... everything is highly limitless, it depends on what kind of data you'd like to represent. Commented Jul 17, 2012 at 16:10

4 Answers 4

4

You are add to much C to do this. This is important to know how to NSMutableArray works and how it's different compare to 2D arrays known from C/C++.

In mutable array you could store another arrays. For example:

NSMutableArray *first = [[NSMutableArray alloc] init];
NSMutableArray *second = [[NSMutableArray alloc] init];

[first addObject:second];

Now you have array in first row in first array! This is something very like C/C++ 2D arrays.

So if you want to add some object to "0,0" you do this:

NSString *mytest = [[NSString alloc] initWithString:@"test"];
[second addObject:mytest];
[first addObject:second];

So now your second contains NSStrings and first contains second. Now you can loop this like you want.

----EDIT: IF you want 1,0 you simply need another instance of second NSMutableArray. For example you have this array:

arr

So here you will be have 3 elements in second array.

NSMutableArray *first = [[NSMutableArray alloc] init];
for(int i =0 ; i < your_size_condition ; i++) {//if you have size, if don't not a problem, you could use while!
   NSArray *second = [[NSArray alloc] initWithObjects:"@something",@"somethingelse",@"more",nil];
   [first addObject:second];
}

You may want to implement NSCopying protocol to do this.

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

1 Comment

hmm, i think i got what you mean, but then what if i want to add at 1,0?
4

If you need a fixed size array, then use plain C array. Before using dynamic ObjC array it needs to create it:

NSMutableArray* array = [NSMutableArray arrayWithCapacity:N];
for(int i=0; i<N; i++) {
    [array addObject:[NSMutableArray arrayWithCapacity:M]];
}

UPD: The following methods might be helpful to work with such array:

[[array objectAtIndex:i] addObject:obj];
[[array objectAtIndex:i] insertObject:obj atIndex:j];
[[array objectAtIndex:i] replaceObjectAtIndex:j withObject:obj];

2 Comments

nope, the sizes are dynamic, i just need to know how to add objects in something like testList[i][n]
in NSMutableArray you don't have to declare size first, you simply add another object. There is more like lists in c++ than arrays.
0

To expand upon @onegray's answer, you can set up some category methods to make soft multidimensional arrays easier to deal with.

MultiMutableArray.h

@interface NSMutableArray(MultiMutableArray)
  -(id)objectAtIndex:(int)i subIndex:(int)s;
  -(void)addObject:(id)o toIndex:(int)i;
@end


@implementation NSMutableArray(MultiMutableArray)

MultiMutableArray.m

#import "MultiMutableArray.h"

-(id)objectAtIndex:(int)i subIndex:(int)s
{
    id subArray = [self objectAtIndex:i];
    return [subArray isKindOfClass:NSArray.class] ? [subArray objectAtIndex:s] : nil;
}

-(void)addObject:(id)o toIndex:(int)i
{
    while(self.count <= i)
        [self addObject:NSMutableArray.new];
    NSMutableArray* subArray = [self objectAtIndex:i];
    [subArray addObject: o];
}

@end

example, MultiArrayTests.m

#import <SenTestingKit/SenTestingKit.h>

#import "MultiMutableArray.h"

@interface MultiArrayTests : SenTestCase
@end

@implementation MultiArrayTests

-(void)testMultiArray
{
    NSMutableArray* a = NSMutableArray.new;
    [a addObject:@"0a" toIndex:0];
    [a addObject:@"0b" toIndex:0];
    [a addObject:@"0c" toIndex:0];
    [a addObject:@"1a" toIndex:1];
    [a addObject:@"1b" toIndex:1];
    [a addObject:@"2a" toIndex:2];
    STAssertEquals(a.count, 3U, nil);
    NSMutableArray* a1 = [a objectAtIndex:0];
    NSMutableArray* a2 = [a objectAtIndex:1];
    NSMutableArray* a3 = [a objectAtIndex:2];
    STAssertEquals(a1.count, 3U, nil);
    STAssertEquals(a2.count, 2U, nil);
    STAssertEquals(a3.count, 1U, nil);
}

@end

I have written further details at http://peterdeweese.tumblr.com/post/27411932460/soft-multi-dimensional-arrays-in-objective-c

Comments

0
[[testList objectAtIndex:i] insertObject:item atIndex:j];

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.