gpt4 book ai didi

ios - Objective C 创建未知大小的二维数组并访问它

转载 作者:行者123 更新时间:2023-11-29 11:39:35 25 4
gpt4 key购买 nike

我对 Objective-C 还是个新手。我想创建一个未知大小的二维数组,以便我可以访问它并添加值。我来自 C++ 背景并查看教程点和示例,例如 Declaring and Initializing 2D array of unknown size in C看起来 C++ 和 Objective C 在创建二维数组方面有相似之处。

我实现了以下示例,但出现错误:

由于未捕获的异常“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 < categories.count; i++)

{
NSArray* subCategories= [categories[i] objectForKey: @"subCategories"];


NSDictionary *categoryItems = [categories objectAtIndex:i];
NSString *category = [categoryItems objectForKey:@"cv"];
NSLog(@"%@",category);
[self.categoryCv addObject:category];
NSString *categoryNum = [categoryItems objectForKey:@"ck"];
[self.categoryCk addObject:categoryNum];

if ([subCategories count] > 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] <= j) {
[self.subcategoryCk[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];

NSArray* outcome =[[subCategories objectAtIndex:j] objectForKey:@"outcomes"];

if ([outcome 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

否则,你的问题会崩溃。

为了防止它,你需要添加一个新的NSMutableArraysubcategoryCv什么时候subcategoryCv.count <= i并添加一个新的 NSMutableArraysubcategoryCv[i]什么时候subcategoryCv[i].count <= j .

另一个二维数组也是一样。例如 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];

关于ios - Objective C 创建未知大小的二维数组并访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47324368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com