Javascript 如何创建二维数组以及大小在objective-c中作为变量给出
我不熟悉objective-c。我在2D数组中遇到问题。因为我有一些javascript知识。我将尝试用javascript解释它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
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。。。