简体   繁体   English

我的NSMutablearray是否添加对象,而我的NSLog是否在数组中添加了正确数量的对象?

[英]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. 我想单击一个按钮,并让该按钮将适当的对象添加到NSMutable数组中。 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. 我有一个NSUInteger来查看对象是否被正确添加,但是我正在数组1的NSlog对象中接收,每当我单击另一个按钮添加另一个对象时,我都会收到相同的消息,但是它的NSlog再次打印1,所以我没有确保是否正确添加了对象。

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

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. 最好的方法是将其移动到viewDidLoad方法,它应该可以完成工作。

You need to make the array a property of the view controller: 您需要使数组成为视图控制器的属性:

MyViewController.m: MyViewController.m:

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

Which is initialized in viewDidLoad : 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"];
}

A prettier design for this would be to use tags and a dictionary instead of if-else, so 一个更漂亮的设计是使用标签和字典而不是if-else,因此

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM