0

我如何生成一个二维 NSMutable 数组,如下所示:

大批:

=>[item1]=>[item1a,item1b,item1c...]
=>[item2]=>[item2a,item2b,item2c...]
...
=>[item10]=>[item10a,item10b,item10c...]

到目前为止,我只成功了 [item1]=>[item1a,item1b,item1c...]

当我尝试添加更多二维数组时,它会不断覆盖第一行。

4

4 回答 4

5

创建NSMutableArray并分配NSMutableArrays 作为它的对象。

例如:

NSMutableArray * myBig2dArray = [[NSMutableArray alloc] init];

// first internal array
NSMutableArray * internalElement = [[[NSMutableArray alloc] init] autorelease];
[internalElement addObject:@"First - First"];
[internalElement addObject:@"First - Second"];
[myBig2dArray addObject:internalElement];

// second internal array
internalElement = [[[NSMutableArray alloc] init] autorelease];
[internalElement addObject:@"Second - First"];
[internalElement addObject:@"Second - Second"];
[myBig2dArray addObject:internalElement];
于 2012-05-10T04:33:24.673 回答
3

要制作二维数组,您将制作一个数组数组。

NSArray *2darray = [NSArray arrayWithObjects: [NSArray arrayWithObjects: @"one", @"two", nil], NSArray arrayWithObjects: @"one_2", @"two_2", nil]];

它变得非常冗长,但这是我知道如何做到这一点的方式。根据您的需要,一系列字典可能更适合您的情况。

于 2012-05-10T04:33:49.720 回答
0

我写了一个NSMutableArray包装器,方便用作二维数组。它在 github 上可用,如下所示CRL2DArrayhttps://github.com/tGilani/CRL2DArray

于 2013-04-21T10:23:28.540 回答
0

首先你要在 .h 文件上设置一个 NSMutableDictionary

        @interface MSRCommonLogic : NSObject
        {
            NSMutableDictionary *twoDimensionArray;
        }

        then have to use following functions in .m file


        - (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
        {
            if(!twoDimensionArray)
            {
                twoDimensionArray =[[NSMutableDictionary alloc]init];
            }

            NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
            [twoDimensionArray setObject:value forKey:strKey];

        }

        - (id)getValueFromArray :(int)rows cols:(int) col
        {
            NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
            return  [twoDimensionArray valueForKey:strKey];
        }
于 2014-12-02T10:44:16.587 回答