2

I've created a simple quiz-app which will count how many questions the user has answered correct and put them in a label in another view. Problem is that when the user answers the last question(array) correct, it won't count up in my counter like it does with the other questions, but only switch view. So instead of showing 4 correct in the next view, it will only show 3, if the last question was answered correctly. Still haven't figured. Any help? My code:

func pickQuestion(){

    if questions.count > 0{

        qNumber = random() % questions.count
        questions.shuffleInPlace()
        qLabel.text = questions[qNumber].question

        answerNumber = questions[qNumber].answer

        for i in 0..<buttons.count{
            buttons[i].setTitle(questions[qNumber].answers[i], forState: .Normal)

        }

        questions.removeAtIndex(qNumber)   
    }

    else {

       NSLog("done")

       self.performSegueWithIdentifier("Segue1", sender: self)
    }  
}

Button code, where I check whether the answer is correct or not:

@IBAction func btn1(sender: AnyObject) {

    if answerNumber == 0 {
        pickQuestion()
        count += 1
    }
    else {
        NSLog("Wrong")
        pickQuestion()
    }
}

After a bit of debugging I found out that questions.removeAtIndex(qNumber) is the one removing the array (removeAtIndex is used to prevent questions appear again when already answered)

I am declaring qNumber as:

qNumber = random() % questions.count

How can I set qNumber to be as the current array that gets removed after it has been answered?

I am switching the view in:

else {

   NSLog("done")

   self.performSegueWithIdentifier("Segue1", sender: self)
}  
7
  • 2
    No relevant, but you should follow the swift coding-convention for 'variable names' ie the camelCase. All your variables are starting with capital character, ie AnswerNumber, QNumber etc. Commented Sep 3, 2016 at 23:36
  • Can you add in the code for when the answer is correct and you increase the value of the counter. I'm thinking that you're just not adding to the counter at the right point, and are instead switching views before you call whatever increases the counter Commented Sep 3, 2016 at 23:36
  • You pasted the code for displaying a question, but not for evaluating the answer Commented Sep 3, 2016 at 23:41
  • 2
    I didn't even understand your question, But just to put it out, arrays go from 0 <--> array.count-1 Commented Sep 3, 2016 at 23:47
  • Added the buttoncode in the question. Thanks in advance ! Commented Sep 4, 2016 at 1:22

1 Answer 1

1

After displaying the last question, you remove it from the array, making the array empty. Then, upon answering, you have:

 if answerNumber == 0 {
      pickQuestion()
      count += 1
}

The code in the function checks to see if questions.count is greater than 0, which it isn't, and performs the segue. The segue happens before the count gets updated. Try changing the block to allow the count to be updated first:

if answerNumber == 0 {
    count += 1
    pickQuestion()
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. it works now. Counting first did the job!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.