objective-c - 更改 NSMutableArray 中所有对象的属性

标签 objective-c properties nsstring nsmutablearray

我有一个包含 9 个对象的 NSMutableArray。这些对象有很多属性,比如 questionNamequestionWhatRow 等。

我希望能够更改 NSMutableArray 中的All 对象的其中一个属性。

这是我的 NSMutableArray 类。

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

现在,我有另一个名为 ScoreViewController 的类,我希望能够更改 NSMutableArray 的属性 questionCompleteOrNot questionsArray 用于所有对象/问题到@"No"

我的 ScoreView 有一个重置按钮,但我不知道该放什么来更改 questionsArrayall questionCompleteOrNot 属性> 到 @"No"

很抱歉问了这么长的问题,我想说得非常具体。

编辑

这是我的 tableView 的 DetailView。

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

关于objective-c - 更改 NSMutableArray 中所有对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14106023/

相关文章:

iphone - 我应该如何构建我的 .plist 数组和字典?

ios - 如何提取iOS日历中的假期?

nsstring - 子类化 NSString

ios - 填充 NSString 在 UITableViewCell 中不起作用

nsstring - 替换 sizeWithFont :ForWidth:lineBreakMode:

iphone - initWithNibName : bundle: always load English nib

iphone - 如何创建五个带标签的 UIPickerView?以及如何在 iPhone 中为每个 UIPickerView 赋予不同的名称?

c# - 有没有办法判断一个对象是否在 C# 中显式实现了 ToString

c# - 只允许属性的递增和递减

java - 将 while 循环中的项目添加到 ArrayList java