Most of them are arrays, and I think as long as anyone who studies high numbers has heard of them and used them, multi-dimensional arrays often appear in programs. Many people in university have learned simple C language, and multi-dimensional arrays in C language are like this:
int a[][];
Copy the code
We generally define the scope;
However, in Object-C, there is no such concept as multi-dimensional array. Even if object-C is compatible with C language, C multi-dimensional array can not meet the type requirements of iOS. Therefore, it is impossible to use C writing method to apply iOS.
When multi-dimensional arrays appear, as we understand them, they are arrays within arrays, so let’s try something like this:
NSMutableArray *mainArray = [[NSMutableArray alloc]init];
NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@"4",@"5",@"6",nil];
NSMutableArray *array3 = [NSMutableArray arrayWithObjects:@"7",@"8",@"9",nil];
[mainArray addObject:array1];
[mainArray addObject:array2];
[mainArray addObject:array3];
Copy the code
Run add a breakpoint to see the data:
So that’s what we get when we run it, and it looks fine, but what if WE want to access the first array? See the following:
NSMutableArray *oneArray = (NSMutableArray *)mainArray[0];
Copy the code
To extract the first of these values:
NSString *firstStr = ((NSMutableArray *)mainArray[0])[0];
Copy the code
The above is for the convenience of everyone to see the writing method, so do not directly use oneArray[0].
If I were to replace one of the values, it would be self-explanatory:
((NSMutableArray *)mainArray[0])[0] = @"hello";
Copy the code
If you need to upload multidimensional array information in the interface data, for example:
This is an interface to submit evaluation, as we know, multiple goods order, goods should be evaluated together, here is a multidimensional array, upload data how to transfer?
for (int i = 0; i < _o_model.goods.count; i++) {
OrderGoodsModel *model = [OrderGoodsModel yy_modelWithJSON:[_o_model.goods objectAtIndex:i]];
NSString *goodsKey=[NSString stringWithFormat:@"goodsevaluate[%d][ordergoodsid]",i];
NSString *contentKey=[NSString stringWithFormat:@"goodsevaluate[%d][content]",i];
[dic setObject:model.ordergoodsid forKey:goodsKey];
[dic setObject:contentList[i] forKey:contentKey];
}
Copy the code
The above is the use of multi-dimensional array as a POST parameter, just a format, unknown parameter name please ignore directly.
Here, the use of multi-dimensional array in iOS has come to an end, basic usage here has been very comprehensive, if you have any questions, welcome to leave a comment or add group discussion, group number please see the home page.