简体   繁体   English

在Multidimensional NSMutableArray中添加对象

[英]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. 你添加了很多C来做到这一点。 This is important to know how to NSMutableArray works and how it's different compare to 2D arrays known from C/C++. 这对于了解NSMutableArray如何工作以及与C / C ++中已知的2D数组相比有何不同非常重要。

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. 这与C / C ++ 2D数组非常相似。

So if you want to add some object to "0,0" you do this: 因此,如果要将某个对象添加到“0,0”,请执行以下操作:

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

So now your second contains NSStrings and first contains second . 所以现在你的第二个包含NSStrings第一个包含第二个 Now you can loop this like you want. 现在你可以按照你想要的方式循环。

----EDIT: IF you want 1,0 you simply need another instance of second NSMutableArray. ----编辑:如果你想要1,0你只需要第二个 NSMutableArray的另一个实例。 For example you have this array: 例如,你有这个数组:

ARR

So here you will be have 3 elements in second array. 所以这里你将在第二个数组中有3个元素。

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. 您可能希望实现NSCopying协议来执行此操作。

If you need a fixed size array, then use plain C array. 如果需要固定大小的数组,则使用纯C数组。 Before using dynamic ObjC array it needs to create it: 在使用动态ObjC数组之前,需要创建它:

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: UPD:以下方法可能有助于使用此类数组:

[[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. 为了扩展@ onegray的答案,您可以设置一些类别方法,以使软多维数组更容易处理。

MultiMutableArray.h 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 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 例如,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 我在http://peterdeweese.tumblr.com/post/27411932460/soft-multi-dimensional-arrays-in-objective-c上写了更多详细信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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