iosnsstringnsmutablearraylogistics

Prevent choosing of certain objects in NSMutableArray?


I am kind of in a pickle here since I have tried many things to accomplish this and have failed.

What I want to do: I have a NSString in my app which is the answer to a question to a round in a quiz. My answers are stored in a NSMutableArray. These answers are chosen at random. I have 4 buttons in my app that are options (possible answers) for the round. I don't want to have multiple answers!

What I have tried:

  1. I have tried deleting the answer after storing it in one of the buttons so that it couldn't be chosen again but it ended up causing a crash since I was trying to delete a object in the array while using fast enumeration (for loop).
  2. I have tried just detecting if the button title was equal to to the answer after setting the correct answer to a specific button but that didn't work for some odd reason (no crash). There will still multiple buttons with the same answer

What I need help with: How should I stop the answer from being in multiple buttons so that the quiz isn't showing the obvious answer?

What should I do instead?

Thanks!

Edit1: So I pretty much made a NSArray of my four UIButtons.

I put the answer into a random UIButton like this:

NSInteger chosen = (arc4random() % 4);
UIButton *randButton = (UIButton *)[buttonArray objectAtIndex:chosen];
[randButton setTitle:imageName forState:UIControlStateNormal];

Then I title the other buttons like so, my buttons that do not have the answer have a title of nothing so I do this:

- (void)titleButtons {
    for (UIButton *buttons in buttonArray) {
        if ([[buttons titleForState:UIControlStateNormal] == nil]) {
            UIButton *button = buttons;
            NSString *superheroString = (NSString*)[superheroArray objectAtIndex:(arc4random() % [superheroArray count])];
            [button setTitle:superheroString forState:UIControlStateNormal];
            [self checkTitles:button];
        }
    }

Then the checkTitle method looks like this, this is the method where I try to make sure where 2 buttons do not have the same answer as the imageName (answer):

- (void)checkTitles:(UIButton*)button {
    if ([[button titleForState:UIControlStateNormal] isEqualToString:imageName]) {
        //Duplicate Answer so re-title button
        NSString *newTitle = [superheroArray objectAtIndex:(arc4random() % [superheroArray count])];
        [button setTitle:newTitle forState:UIControlStateNormal];
        //Call same method again to see if new title is still same answer as pic to avoid same answers
        [self checkTitles:button];
    }
}
}

Solution

  • If you have an NSArray with all your answers, and you want 1 correct answer and 3 different wrong answers, you can do the following:

    1. Decide in which button do you want the right answer. (randomize it)
    2. Get a random answer from your array and store the index of that answer in a temporary array.
    3. Get another random answer and make sure you are not picking an answer with the same index as the one you have in your temp array, (and again, store the new index in the temporary array)