0

I'm trying to add NSMutableArray to other NSMutableArray, a mutable array set as instance ivar, but I allways got the array arrayPlayoff empty. Other thing that I have detected, is that when debugging in the attached method, local variables are not shown in the debugger variables section, even selecting "local" option.

-(void)loadGamesPlayoffs{

    NSMutableArray *eli1 = [[NSMutableArray alloc] init];

    NSMutableArray *eli2 =[[NSMutableArray alloc] init];

    NSMutableArray *eli3 = [[NSMutableArray alloc] init];

    NSMutableArray *eli4 = [[NSMutableArray alloc] init];

    for (NSDictionary *eliminatoria in copaReyArray){

        int eli = [[eliminatoria valueForKey:@"eliminatoria"]integerValue];

        NSLog(@"eli %d", eli);

        if (eli==1){

            [eli1 addObject:eliminatoria];

        } else if (eli==2){

            [eli2 addObject:eliminatoria];

        }else if (eli==3){

            [eli3 addObject:eliminatoria];

        } else if (eli==4){

            [eli4 addObject:eliminatoria];

        }


    }


    [arrayPlayOff addObject:eli1];

    [arrayPlayOff addObject:eli2];

    [arrayPlayOff addObject:eli3];

    [arrayPlayOff addObject:eli4];


   }

 many thanks   
theomen
  • 913
  • 3
  • 20
  • 39
  • possible duplicate of [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/questions/7125326/cannot-add-items-to-an-nsmutablearray-ivar), http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working, http://stackoverflow.com/questions/3683761/nsmutablearray-addobject-not-affecting-count – jscs May 22 '12 at 17:00
  • 2
    Seems like you are not initialising `arrayPlayOff` which is causing issue? – rishi May 22 '12 at 17:01
  • Guys, many thanks! can you post your answer to vote you? I had wrong idea that ivar are not necessary to initialize..... – theomen May 22 '12 at 17:07
  • @theomen -- There is a big difference between "initializing a variable" -- setting it to some known value -- and creating the object to which the variable is intended to point. All ivars in Objective-C are automatically "initialized" in the sense that they're set to zero/nil when the object is created. But if the ivar is intended to address an object such as an NSArray, the object is ***not*** created. – Hot Licks May 22 '12 at 19:41

1 Answers1

1

If arrayPlayOff is an Instance Variable you could use properties, your .h should look something like this:

#import <UIKit/UIKit.h>
@interface YourClassViewController : UIViewController{
   NSMutableArray *arrayPlayOff;
   //Some other variables
}

@property (nonatomic, retain) NSMutableArray *arrayPlayOff;

//Some other methods

@end

Then in your .m file you could use:

@implementation YourClassViewController

@synthesize arrayPlayOff;

- (NSMutableArray *)arrayPlayOff{
  if(!arrayPlayOff){
    arrayPlayOff = [[NSMutableArray alloc] init];
  }
  return arrayPlayOff;
}

//Your other methods

//Overwrite the dealloc function so you don't have any memory leaks
- (void)dealloc{
  [arrayPlayOff release];
  [super dealloc];
}

@end

Now you could access your variable as

self.arrayPlayOff

and this way you can be 100% sure that your variable is always initialized.

Your method could look like this (Also you should release your other NSMutableArrays since you are not using after adding them to the arrayPlayOff array):

-(void)loadGamesPlayoffs{

    NSMutableArray *eli1 = [[NSMutableArray alloc] init];

    NSMutableArray *eli2 =[[NSMutableArray alloc] init];

    NSMutableArray *eli3 = [[NSMutableArray alloc] init];

    NSMutableArray *eli4 = [[NSMutableArray alloc] init];

    for (NSDictionary *eliminatoria in copaReyArray){

        int eli = [[eliminatoria valueForKey:@"eliminatoria"]integerValue];

        NSLog(@"eli %d", eli);

        if (eli==1){

            [eli1 addObject:eliminatoria];

        } else if (eli==2){

            [eli2 addObject:eliminatoria];

        }else if (eli==3){

            [eli3 addObject:eliminatoria];

        } else if (eli==4){

            [eli4 addObject:eliminatoria];

        }


    }


    [self.arrayPlayOff addObject:eli1];

    [self.arrayPlayOff addObject:eli2];

    [self.arrayPlayOff addObject:eli3];

    [self.arrayPlayOff addObject:eli4];

    //Releasing the NSMutableArrays
    [eli1 release]; 
    [eli2 release]; 
    [eli3 release]; 
    [eli4 release];

}
ivaN
  • 191
  • 6