简体   繁体   中英

Change a Property for All Objects in an NSMutableArray

I have an NSMutableArray with 9 objects. Those objects have many properties, like questionName , questionWhatRow , etc.

I want to be able to change one of those properties for All of the objects in the NSMutableArray .

Here is my NSMutableArray Class.

-(id) init
{
    self = [super init];

    if (self)
    {
        self.questionsArray = [[NSMutableArray alloc] init];

        Question *newQuestion = [[Question alloc] init];
        newQuestion.questionName = @"What is the name of the girl who has to fetch water for her family every day?";
        newQuestion.questionRowName = @"Question 1";
        newQuestion.questionAnswer = @"Nya";
        newQuestion.questionHint = @"Maybe if you read the book you would know";
        newQuestion.questionWhatRow = @"Nya";
        newQuestion.questionCompleteOrNot = @"No";
        newQuestion.pointForQuestion = 1;
        [self.questionsArray addObject:newQuestion];

        newQuestion = [[Question alloc] init];
        newQuestion.questionName = @"What is Nya's sister's name?";
        newQuestion.questionRowName = @"Question 2";
        newQuestion.questionAnswer = @"Akeer";
        newQuestion.questionHint = @"Maybe if you read the book you would know";
        newQuestion.questionWhatRow = @"Nya";
        newQuestion.questionCompleteOrNot = @"No";
        newQuestion.pointForQuestion = 1;
        [self.questionsArray addObject:newQuestion];

        newQuestion = [[Question alloc] init];
        newQuestion.questionName = @"What people is Nya's mother scared of when her father and sons go hunting?";
        newQuestion.questionRowName = @"Question 3";
        newQuestion.questionAnswer = @"Dinka";
        newQuestion.questionHint = @"Maybe if you read the book you would know";
        newQuestion.questionWhatRow = @"Nya";
        newQuestion.questionCompleteOrNot = @"No";
        newQuestion.pointForQuestion = 1;
        [self.questionsArray addObject:newQuestion];

        newQuestion = [[Question alloc] init];
        newQuestion.questionName = @"What is Salva scared of when he is in the Akobo desert?";
        newQuestion.questionRowName = @"Question 4";
        newQuestion.questionAnswer = @"Lions";
        newQuestion.questionHint = @"He is scared of an animal because it ate his friend Marial";


        newQuestion = nil;

    }

    return self;
}

-(NSUInteger)count
{
    return questionsArray.count;
}


- (Question *)questionAtIndex:(NSUInteger)index
{
    return [questionsArray objectAtIndex:index];
}

Now, I have another class called ScoreViewController , and I want to be able to change the property, questionCompleteOrNot of the NSMutableArray questionsArray for all objects/questions to @"No" .

My ScoreView has a button to Reset, but I don't know what to put in it to change all questionCompleteOrNot properties of the questionsArray to @"No" .

Sorry for the long question, I was trying to be very specific.

Edit

Here is my DetailView for a tableView.

if ([[selectedQuestion questionCompleteOrNot] isEqualToString:@"No"])
    {
        if ([[selectedQuestion questionAnswer] caseInsensitiveCompare:answerField.text] == NSOrderedSame)
        {
            // Show the  correct label
            [correctLabel setHidden:NO];
            correctLabel.text = @"Correct!";
            correctLabel.textColor = [UIColor greenColor];

        }
        else
        {
            // Show the incorrect label
            [correctLabel setHidden:NO];
            correctLabel.text = @"Incorrect";
            correctLabel.textColor = [UIColor redColor];
            [incorrectPlayer play];
            [[selectedQuestion questionCompleteOrNot] isEqualToString:@"Yes"];

        }

        // Erase the text in the answerField
        answerField.text = @"";
    }
    if ([[selectedQuestion questionCompleteOrNot] isEqualToString:@"Yes"])
    {
        UIAlertView *error = [[UIAlertView alloc]
                              initWithTitle:@"Error"
                              message:@"You already answered this question. Please click the reset button on the score view to restart."
                              delegate:self
                              cancelButtonTitle:@"Okay!"
                              otherButtonTitles:nil];

        [error show];
        answerField.text = @"";
    }

    selectedQuestion.questionCompleteOrNot = @"Yes";
    correctLabel.text = @"";
    NSLog(@"Selected question's questionCompleteOrNot is %@", [selectedQuestion questionCompleteOrNot]);

您可以使用NSArraymakeObjectsPerformSelector:withObject: ,如下所示:

[questionsArray makeObjectsPerformSelector:@selector(setQuestionCompleteOrNot:) withObject:@"No"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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