Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在Objective C中生成二维数组_Objective C_Ios_Xcode - Fatal编程技术网

Objective c 如何在Objective C中生成二维数组

Objective c 如何在Objective C中生成二维数组,objective-c,ios,xcode,Objective C,Ios,Xcode,如何生成二维NSMutable数组,如下所示: 数组: =>[item1]=>[item1a,item1b,item1c...] =>[item2]=>[item2a,item2b,item2c...] ... =>[item10]=>[item10a,item10b,item10c...] 到目前为止,我只在[item1]=>[item1a、item1b、item1c…]之前取得了成功 当我尝试添加更多二维数组时,它会一直覆盖第一行。创建NSMutable

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

数组:

=>[item1]=>[item1a,item1b,item1c...]
=>[item2]=>[item2a,item2b,item2c...]
...
=>[item10]=>[item10a,item10b,item10c...]
到目前为止,我只在[item1]=>[item1a、item1b、item1c…]之前取得了成功


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

创建
NSMutableArray
并将
NSMutableArray
作为其对象分配给它

例如:

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

要创建二维数组,您需要创建一个数组数组

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

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

我编写了一个
NSMutableArray
包装器,以便作为二维数组使用。它在github上的位置为
CRL2DArray

首先要在.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];
        }

请继续关注,对clang的一些添加将允许一些用于创建数组的速记语法,这将使多维数组更容易。语法看起来像
array=@[@[item1a,item1b],@[item2a,item2b]。这可能是下一版本Xcode的一部分。这个问题会被问多少次@理查德:你链接的问题不是关于
NSArray
s中的
NSArray
s。@JacquesCousteau它是关于objective-c中的二维数组。答案也可以用于对象。@RichardJ.RossIII:除了对象没有明确的所有权。如果它提供了一种以一维方式访问二维数组的方法,那么这将非常有用。示例:数组{橙色、梨色}{红色、蓝色、绿色}{正方形、圆形}。。阵列1d[3]=蓝色