kaddie
kaddie

Reputation: 283

Objective C Create 2D array of unknown size and access it

I am still new to objective C. I would like to create a 2D array of unknown size such that i can access it and add values. I come from a C++ background and looking at tutorial point and examples such as Declaring and Initializing 2D array of unknown size in C it seems there is a similarity in creation of a 2D array between C++ and Objective C.

I have implemented the following example and i get an error:

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'**

I am not sure what is causing the error, i would be grateful for assistance in solving this error and understanding it.

-(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;
}

Then the loops to create the arrays:

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];

}

This is how i try to access the array :

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

Upvotes: 1

Views: 129

Answers (1)

trungduc
trungduc

Reputation: 12144

When you call subcategoryCv[i][j], you have to make sure that

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

if not, you will got crash in you question.

To prevent it, you need to add a new NSMutableArray to subcategoryCv when subcategoryCv.count <= i and add a new NSMutableArray to subcategoryCv[i] when subcategoryCv[i].count <= j.

It's same with another 2D array. For example with 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];

Upvotes: 1

Related Questions