Objective C创建未知大小的2D数组并访问它

我仍然是目标C的新手。我想创建一个未知大小的2D数组,以便我可以访问它并添加值。 我来自C ++背景并查看教程点和示例,例如在C中声明和初始化未知大小的2D数组,似乎在C ++和Objective C之间创建2D数组有相似之处。

我已经实现了以下示例,我收到一个错误:

因未捕获的exception’NSRangeException’而终止应用程序,原因:’* – [__ NSArrayM objectAtIndex:]:索引0超出空数组的边界’**

我不确定是什么导致错误,我将非常感谢帮助解决此错误并理解它。

-(NSMutableArray *)categoryCk { if (!_categoryCk) { _categoryCk = [[NSMutableArray alloc] init]; } return _categoryCk; } -(NSMutableArray *)subcategoryCv { if (!_subcategoryCv) { _subcategoryCv = [[NSMutableArray alloc] init]; } return _subcategoryCv; } -(NSMutableArray *)subcategoryCk { if (!_subcategoryCk) { _subcategoryCk = [[NSMutableArray alloc] init]; } return _subcategoryCk; } -(NSMutableArray *)outcomeCv { if (!_outcomeCv) { _outcomeCv = [[NSMutableArray alloc] init]; } return _outcomeCv; } -(NSMutableArray *)outcomeCk { if (!_outcomeCk) { _outcomeCk = [[NSMutableArray alloc] init]; } return _outcomeCk; } 

然后循环创建数组:

 if([categories count]>0){ for (int i =0; i  0){ for (int j = 0; j < subCategories.count; j++) { if (self.subcategoryCv.count <= i) { [self.subcategoryCv addObject:[[NSMutableArray alloc] init]]; } if ([self.subcategoryCv[i] count] <= j) { [self.subcategoryCv[i] addObject:[[NSMutableArray alloc] init]]; } if (self.subcategoryCk.count <= i) { [self.subcategoryCk addObject:[[NSMutableArray alloc] init]]; } if ([self.subcategoryCk[i] count]  0) { for (int k = 0; k < outcome.count; k++) { if (self.outcomeCv.count <= j) { [self.outcomeCv addObject:[[NSMutableArray alloc] init]]; } if ([self.outcomeCv[j] count] <= k) { [self.outcomeCv[j] addObject:[[NSMutableArray alloc] init]]; } if (self.outcomeCk.count <= j) { [self.outcomeCk addObject:[[NSMutableArray alloc] init]]; } if ([self.outcomeCk[j] count] <= k) { [self.outcomeCk[j] addObject:[[NSMutableArray alloc] init]]; } NSDictionary *outComeItems = [outcome objectAtIndex:k]; NSString *outcomeCategoryCV = [outComeItems objectForKey:@"cv"]; //NSLog(@"%@",outcomeCatCV); [self.outcomeCv[j][k] addObject:outcomeCategoryCV]; NSString *outCatNum = [outComeItems objectForKey:@"ck"]; [self.outcomeCk[j][k] addObject:outCatNum]; } }else{ UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Error loading data from service : Empty Outcomes data" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } } } else{ UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Error loading data from service : Empty Sub-Categories data" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } } }else{ UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Error loading data from service : Empty Categories data" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } 

这是我尝试访问数组的方式:

  cell.textLabel.text = [self.outcomeCv[categoryIndex][subcategoryIndex] objectAtIndex:indexPath.row] ; 

当你调用subcategoryCv[i][j] ,你必须确保这一点

subcategoryCv.count > i && subcategoryCv[i].count > j

如果没有,你会在你的问题上崩溃。

为防止这种情况,您需要在subcategoryCv.count <= i时将新的NSMutableArray添加到subcategoryCv ,并在subcategoryCv[i].count <= j时将新的NSMutableArray添加到subcategoryCv[i]

它与另一个2Darrays相同。 例如,使用subcategoryCv

 if (self.subcategoryCv.count <= i) { [self.subcategoryCv addObject:[[NSMutableArray alloc] init]]; } if ([self.subcategoryCv[i] count] <= j) { [self.subcategoryCv[i] addObject:[[NSMutableArray alloc] init]]; } NSDictionary *subcategoryItems = [subCategories objectAtIndex:j]; NSString *subCategoryCv = [subcategoryItems objectForKey:@"cv"]; [self.subcategoryCv[i][j] addObject:subCategoryCv]; NSString *subCatNum = [subcategoryItems objectForKey:@"ck"]; [self.subcategoryCk[i][j] addObject:subCatNum];