currentQuestionIndex Display Order


#1

The first line of the showQuestion method, self.currentQuestionIndex++;, causes the second element of the questions and answers arrays to appear first when the app is started. Was this done on purpose or is it an oversight?


#2

@drhymes2931 This was bothering me aswell, tried to work out a solution and came up with the below:

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.currentQuestionIndex = -1;
    }

by setting the currentQuestionIndex to -1 on first launch this sets the currentQuestionIndex to 0 the first time you press the Question Button.

not sure if this is the correct fix but that’s what I came up with, interested if anyone has any feedback on this.


#3

It was done on purpose to simplify code. Either initializing currentQuestionIndex to -1, or displaying the 0th question in viewDidLoad would show the 0th question first (I prefer the latter).

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *question = self.questions[self.currentQuestionIndex];
    self.questionLabel.text = question;
}

#4

I believe I found a simpler solution - slight rewrite of methods - not requiring setting the index to -1 (also posted under new topic ‘Logic Error’ - didn’t see this topic before):

  • (IBAction)showQuestion:(id)sender
    {
    // Am I past the last question?
    if (self.currentQuestionIndex == [self.questions count]) {

      // Go back to the first question
      self.currentQuestionIndex = 0;
    

    }

    // Get the string at that index in the questions array
    NSString *question = self.questions[self.currentQuestionIndex];

    // Display the string in the question label
    self.questionLabel.text = question;

    // Reset the answer label
    self.answerLabel.text = @"???";
    }

  • (IBAction)showAnswer:(id)sender
    {
    // What is the answer to the current question?
    NSString *answer = self.answers[self.currentQuestionIndex];

    // Display it in the answer label
    self.answerLabel.text = answer;

    //Increment index in questions array for next question
    self.currentQuestionIndex++;
    }


#5

[quote=“MichaelB”]I believe I found a simpler solution - slight rewrite of methods - not requiring setting the index to -1 (also posted under new topic ‘Logic Error’ - didn’t see this topic before):

  • (IBAction)showQuestion:(id)sender
    {
    // Am I past the last question?
    if (self.currentQuestionIndex == [self.questions count]) {

      // Go back to the first question
      self.currentQuestionIndex = 0;
    

    }

    // Get the string at that index in the questions array
    NSString *question = self.questions[self.currentQuestionIndex];

    // Display the string in the question label
    self.questionLabel.text = question;

    // Reset the answer label
    self.answerLabel.text = @"???";
    }

  • (IBAction)showAnswer:(id)sender
    {
    // What is the answer to the current question?
    NSString *answer = self.answers[self.currentQuestionIndex];

    // Display it in the answer label
    self.answerLabel.text = answer;

    //Increment index in questions array for next question
    self.currentQuestionIndex++;
    }[/quote]

With this approach, if you click “Show Answer” multiple times in a row, you’ll see incorrect answers. It’ll also crash if you keep tapping “Show Answer” more than a few times.


#6

I appreciate the feedback, Christian.


#7

A quick fix is to add the following to the viewDidLoad method as shown below:

[code]- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

self.currentQuestionIndex--;

}[/code]

You can leave all the other code provided in the book as is.