Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.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
Javascript 如何创建二维数组以及大小在objective-c中作为变量给出_Javascript_Ios_Objective C_Xcode - Fatal编程技术网

Javascript 如何创建二维数组以及大小在objective-c中作为变量给出

Javascript 如何创建二维数组以及大小在objective-c中作为变量给出,javascript,ios,objective-c,xcode,Javascript,Ios,Objective C,Xcode,我不熟悉objective-c。我在2D数组中遇到问题。因为我有一些javascript知识。我将尝试用javascript解释它 var row = 10; var col = 10; var array[row][col]; for (var i = 0; i < row; i++){ for (var j = 0; j < col; j++){ //do something in here } } row = 20; col = 20; f

我不熟悉objective-c。我在2D数组中遇到问题。因为我有一些javascript知识。我将尝试用javascript解释它

var row = 10;
var col = 10;
var array[row][col];

for (var i = 0; i < row; i++){
    for (var j = 0; j < col; j++){
        //do something in here
    }
}

row = 20;
col = 20;

for (var i = 0; i < row; i++){
    for (var j = 0; j < col; j++){
        //do something in here
    }
}
如何在objective-c中对此进行编码?

希望这有助于:

NSInteger row = 10;
NSInteger col = 10;

// Array with variable size. For fixed size, use [[NSMutableArray alloc] initWithCapacity:row]
NSMutableArray* array = [NSMutableArray new];

for (NSInteger i = 0; i < row; i++) {

    // You must add values before you can access them. You cannot access a value at an index which is greater than the size of the array

    NSMutableArray* colArray = [NSMutableArray new];
    [array addObject:colArray];

    for (NSInteger j = 0; j < col; j++) {


        [colArray addObject:someObject];

        // You can access the array like such:
        id object = array[i][j];

        // You can change an existing value in the array using the same notation:
        array[i][j] = someObject;

        // You cannot set an array value to nil or null. Instead use NSNull which is an object you can use to represent a null value:
        array[i][j] = [NSNull null];
    }
}


// You can also initialise an array with the following notation if you know the values in advance:

NSArray* anotherArray = @[objectOne, objectTwo, objectThree];

// Similarly, you can create a 2-dimensional array as follows:

NSArray* twoDimensionalArray = @[
                                 @[rowOneColumnOne, rowOneColumnTwo, rowOneColumnThree],
                                 @[rowTwoColumnOne, rowTwoColumnTwo, rowTwoColumnThree]
                                 ];

在ECMAScript中,var array[row][col]是一个语法错误。在objective c中,声明变量时不指定数组的大小。您只需分配一个NSMutableArray并向其中添加NSMutableArray。。。