目标c创建和访问3维数组

时间:2017-11-22 14:24:10

标签: objective-c arrays multidimensional-array

我正在尝试从Json数据创建一个三维数组,我从api获得但是我得到了这个

  

错误:由于未捕获的异常“NSRangeException”而终止应用,   原因:'* - [__NSArrayM objectAtIndex:]:索引0超出范围   空数组'**

我来自C和C ++背景,我认为在目标c中创建3D数组有点不同。我不确定为什么它会抛出这个错误,任何人都可以协助我解决这个错误并理解它。

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[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[i][j] count] <= k) 
                        {
                            [self.outcomeCv[i][j] addObject:[[NSMutableArray alloc] init]];
                        }

                        if (self.outcomeCk.count <= j) 
                        {
                            [self.outcomeCk addObject:[[NSMutableArray alloc] init]];
                        }
                        if ([self.outcomeCk[i][j] count] <= k) 
                        {
                            [self.outcomeCk[i][j] addObject:[[NSMutableArray alloc] init]];
                        }

                        NSDictionary *outComeItems = [outcome objectAtIndex:k];
                        NSString *outcomeCategoryCV = [outComeItems objectForKey:@"cv"];
                        //NSLog(@"%@",outcomeCatCV);
                        [self.outcomeCv[i][j][k] addObject:outcomeCategoryCV];
                        NSString *outCatNum = [outComeItems objectForKey:@"ck"];
                        [self.outcomeCk[i][j][k] addObject:outCatNum];
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我想那个问题就在那个部分:

if (self.outcomeCv.count <= j) 
{
    [self.outcomeCv addObject:[[NSMutableArray alloc] init]];
}
if ([self.outcomeCv[i][j] count] <= k) 
{
    [self.outcomeCv[i][j] addObject:[[NSMutableArray alloc] init]];
}

同样适用于self.outcomeCk。您尚未正确添加第二级中的对象。你应该做的是:

if (self.outcomeCv.count <= i) 
{
    [self.outcomeCv addObject:[[NSMutableArray alloc] init]];
}
if ([self.outcomeCv[i] count] <= j) 
{
    [self.outcomeCv[i] addObject:[[NSMutableArray alloc] init]];
}
if ([self.outcomeCv[i][j] count] <= k) 
{
    [self.outcomeCv[i][j] addObject:[[NSMutableArray alloc] init]];
}