简体   繁体   中英

Adding objects in Multidimensional NSMutableArray

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 :(

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.

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];

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM