Nijoel
Nijoel

Reputation: 13

Is my NSMutablearray adding the objects and my NSLog the correct number of objects in the array?

Im working on a project and i need some advice. I have this button handler code that handle all my buttons clicks(like a survey). I want to click one button and have the button add the appropriate object to the NSMutable array. now my question is is it safe to use a mutable array or a dictionary since mutable objects can be added or removed. and how can i check if the objects are being added to the array correctly. i have a NSUInteger to see if the objects are being added correctly but i'm receiving in the NSlog objects in the array 1 and whenever i click another button to add another object i receive the same message but its NSlog prints 1 again so im not sure if the objects are being added correctly or not.

-(IBAction)checkBoxButtonHandler:(id)sender
{// array being allocated
    NSMutableArray *arrayOfselections = [NSMutableArray array];


    if (sender == checkButton1)

{

        if (!buttonChecked)
    {

        [arrayOfselections addObject:@"Freshman"];
        NSLog(@"Freshman Checked");

        [checkButton1 setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
        buttonChecked = YES;

    }
    else
    {
        [checkButton1 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
        buttonChecked = NO;
    }


}

if (sender == checkB2)
{
    //    [arrayOfselections addObject:@"Sophmore"];
   // NSLog(@"check 2");

    if (!buttonChecked)
    {
        [arrayOfselections addObject:@"Sophmore"];
        NSLog(@"Sophmore Checked");
        [checkB2 setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
        buttonChecked = YES;

    }
    else
    {
        [checkB2 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
        buttonChecked = NO;
    }

}
    [arrayOfselections accessibilityElementCount];

 if (sender == checkBox3)
    {

        //NSLog(@"check 3");

    if (!buttonChecked)
    {
        [arrayOfselections addObject:@"Junior"];
        NSLog(@"Junior Checked");
        [checkBox3 setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
        buttonChecked = YES;



    }
    else
    {
        [checkBox3 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
        buttonChecked = NO;
    }


    }

  if (sender == checkBx4)
{

    NSLog(@"Checked 4");
    if (!buttonChecked)
    {
        [arrayOfselections addObject:@"Senior"];
        NSLog(@"Senior Checked");
        [checkBx4 setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
        buttonChecked = YES;

    }
    else
    {
        [checkBx4 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
        buttonChecked = NO;
        }
NSUInteger arraySize = [arrayOfselections count];
NSLog(@"Number of stuff inside array: %ui",arraySize);
     }

Upvotes: 0

Views: 142

Answers (3)

trojanfoe
trojanfoe

Reputation: 122381

You need to make the array a property of the view controller:

MyViewController.m:

@interface MyViewController ()
@property (nonatomic) NSMutableArray *arrayOfSelections;
@end

Which is initialized in viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrayOfSelections = [NSMutableArray new];
    ...
}

-(IBAction)checkBoxButtonHandler:(id)sender
{   
    // Remove this line:
    //NSMutableArray *arrayOfselections = [NSMutableArray array];

    ...
    // and change any references to self.arrayOfSelections:
    [self.arrayOfselections addObject:@"Freshman"];
}

Upvotes: 1

danh
danh

Reputation: 62686

A prettier design for this would be to use tags and a dictionary instead of if-else, so

// assign tags:
checkButton1.tag = 1;
checkButton2.tag = 2;

// etc., then setup data beforehand
NSDictionary *mapTags = @{ @1:@"Freshman", @2:@"Sophomore" /* etc */ };
NSMutableArray *arrayOfselections = [NSMutableArray array];

// then look how small this gets:
-(IBAction)checkBoxButtonHandler:(id)sender {

    NSNumber *tagNumber = [NSNumber numberWithInt:sender.tag];
    [self.arrayOfselections addObject:self.mapTags[tagNumber]];
}

Upvotes: 0

Greg
Greg

Reputation: 25459

You create new array every time you press the button, this happened in this line:

NSMutableArray *arrayOfselections = [NSMutableArray array];

You need to create it just once. The best way is just move it to viewDidLoad method, it should do the job.

Upvotes: 2

Related Questions