Problems with randomised array items

Hey guys, so for some reason, I've made a UILabel and it presents questions, so instead of it saying the real question that's meant to come, it says the future question. For example: The UILabel is meant to say 5 + 5 = 55 and if it's true or false, if I say its false the console says I got it wrong and says that the question I just answered is 12 + 0, but then the UILabel's current question the UILabel says the next question is 12 + 0. What's happening and can you help me?


var timer = Timer()
    var countdown : Int = 60
    var allQuestions = QB1().listY1.count
    var questionNumber : Int = 1
    var UserPickedAnswer : Bool = false
    var score : Int = 1
    var consoleNumTracker : Int = 1
    
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var trueButton: UIButton!
    @IBOutlet weak var falseButton: UIButton!
    @IBOutlet weak var IGScoreLabel: UILabel!
    @IBOutlet weak var startButtonOutlet: UIButton!
    @IBOutlet weak var countdownLabel: UILabel!
    @IBOutlet weak var backButton: UIBarButtonItem!


    @IBAction func startButton(_ sender: Any) {
        startButtonOutlet.isHidden = true
        print(QB1().listY1.count)
        questionLabel.text = String(QB1().listY1[questionNumber].questionText)
        countdown = 60
        TimerStart()
    }
    
    func TimerStart() {
        
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(TimerCD), userInfo: nil, repeats: true)
        
    }
    
    @objc func TimerCD() {
        
        countdown = countdown - 1
        countdownLabel.text = String(countdown)
        
        if countdown == 1 {
            countdown = countdown - 1
            timer.invalidate()
            timesUp()
        }
        
    }
    

func timesUp() {
        let TimeUp = UIAlertController(title: "Time's Up!", message: "Congrats, you finished! Your score was \(score)!", preferredStyle: .alert)
        
        let TUAction = UIAlertAction(title: "Back To Home", style: .default, handler: { UIAlertAction in
            self.EnableSeeResult()
        })
        
        TimeUp.addAction(TUAction)
        
        present(TimeUp, animated: false, completion: nil)
    }

    
    /*––––––
     Enable Results Button
     –––––—*/
    
    func EnableSeeResult() {
        IGScoreLabel.text = "Score: " + String(score) + "/41"
        falseButton.isEnabled = false
        trueButton.isEnabled = false
        timer.invalidate()
    }
    
    /*––––––
     Enable Results Button
     –––––—*/
    
    func updateUI1() {
        
        IGScoreLabel.text = "Score: " + String(score)
    }


@IBAction func TrueOrFalseClickedY2(_ sender: AnyObject!) {
        if sender.tag == 1 {
            UserPickedAnswer = true
        }
            
        else if sender.tag == 2 {
            UserPickedAnswer = false
        }
        
        
        
        NQ2()
        questionLabel.text = QB2().listY2[questionNumber].questionText
        updateUI2()
        
    }
Answered by Claude31 in 344418022

Problem comes from the order of statements in NQx() func.


You change the question before checking the current answer.


Replace

    func NQ1() {
        questionNumber = Int(arc4random_uniform(UInt32(Int(QB1().listY1.count))))
        questionLabel.text = QB1().listY1[questionNumber].questionText
        checkAnswer1()
    }


By


    func NQ1() {
        checkAnswer1()
        questionNumber = Int(arc4random_uniform(UInt32(Int(QB1().listY1.count))))
        questionLabel.text = QB1().listY1[questionNumber].questionText
    }


In addition, note that func names should start by lowercase ; and names should be more explicit.


You could also replace Arc4random by the newest Int.random

questionNumber = Int.random(in: 0..<QB1().listY1.count)

And you should also test whether the question was already asked.

Please edit your text properly. It is just impossible to read.


Hey guys, so I've made an array called listY1 containing multiple questions, and each array value contains 2 contents, text (String) and correctanswer (bool).

I've also made an array called UserPickedAnswer (bool) and a UILabel called questionLabel.

So for some reason ther question label Is 1 time before the question text:

for example

(I set a var MyVar : bool = false.

var My2ndVar = 0.

var question = ["1+1=2", "2+2=22"]

and the correct answer for the 1 item array is true and the 2nd if false.


the UIlabel shows that the question is 1+1=2 when really the question is 2+2=22,

so if I answer true then it will be wrong because the UILabel shows1+1=2 but the question is 2+2=22)


I know it's confusing but if you understand, please help.



And use code formatter (<>) for the code.

Make sure you show the code where you set UILabel

Sorry, It's a glitch, it should be fixed now.

You do not show the code where the UILabel is set, so it is difficult to be sure.

Do you use

var question = ["1+1=2", "2+2=22"]

somewhere ?


But most likely you have a synchronization issue, because UILabel is not updated yet, but code has moved after. That's often the case when you have an alert and try to use the result of the alert outside of the alert code.


There are several solution:

- using completion handlers

- setting semaphores


But once again, need to see your code first.

var question = ["1+1=2", "2+2=22"]

was an example but in this case the real array is listY1, sorry I forgot to include this part:


class QB1 {

    var listY1 = [Question]()

    init() {

        listY1.append(Question(text: "9 + 0 = 90", correctAnswer: false))
        listY1.append(Question(text: "2 + 5 = 7", correctAnswer: true)) //1
        listY1.append(Question(text: "1 + 1 = 2", correctAnswer: true)) //2
        listY1.append(Question(text: "1 + 3 = 4", correctAnswer: true)) //3
        listY1.append(Question(text: "0 - 9 = 9", correctAnswer: false)) //4
        listY1.append(Question(text: "7 + 1 = 71", correctAnswer: false)) //5
        listY1.append(Question(text: "2 - 1 = 1", correctAnswer: true)) //6
        listY1.append(Question(text: "5 + 4 = 8", correctAnswer: false)) //7
        listY1.append(Question(text: "1 + 9 = 10", correctAnswer: true)) //8
        listY1.append(Question(text: "7 - 4 = 5", correctAnswer: false)) //9
        listY1.append(Question(text: "0 + 2 = 2", correctAnswer: true)) //10
        listY1.append(Question(text: "1 + 1 = 2", correctAnswer: true))
        listY1.append(Question(text: "1 + 9 = 10", correctAnswer: true))
        listY1.append(Question(text: "10 - 7 = 2", correctAnswer: false))
        listY1.append(Question(text: "8 - 1 = 7", correctAnswer: true))
        listY1.append(Question(text: "5 + 5 = 55", correctAnswer: false)) //15
        listY1.append(Question(text: "2 + 9 = 11", correctAnswer: true))
        listY1.append(Question(text: "7 - 3 = 5", correctAnswer: false))
        listY1.append(Question(text: "0 + 2 = 1", correctAnswer: false))
        listY1.append(Question(text: "3 + 3 = 6", correctAnswer: true))
        listY1.append(Question(text: "2 + 1 = 3", correctAnswer: true))
        listY1.append(Question(text: "9 - 1 = 8", correctAnswer: true))
        listY1.append(Question(text: "5 + 5 = 10", correctAnswer: true))
        listY1.append(Question(text: "5 + 3 = 53", correctAnswer: false))
        listY1.append(Question(text: "8 - 3 = 5 ", correctAnswer: true))
        listY1.append(Question(text: "5 + 1 = 6", correctAnswer: true))
        listY1.append(Question(text: "2 + 5 = 7", correctAnswer: true))
        listY1.append(Question(text: "8 - 1 = 5", correctAnswer: false))
        listY1.append(Question(text: "0 + 3 = 5", correctAnswer: false)) //28
        listY1.append(Question(text: "3 + 5 = 8", correctAnswer: true))
        listY1.append(Question(text: "10 + 2 = 8", correctAnswer: false))
    }

}
Accepted Answer

Problem comes from the order of statements in NQx() func.


You change the question before checking the current answer.


Replace

    func NQ1() {
        questionNumber = Int(arc4random_uniform(UInt32(Int(QB1().listY1.count))))
        questionLabel.text = QB1().listY1[questionNumber].questionText
        checkAnswer1()
    }


By


    func NQ1() {
        checkAnswer1()
        questionNumber = Int(arc4random_uniform(UInt32(Int(QB1().listY1.count))))
        questionLabel.text = QB1().listY1[questionNumber].questionText
    }


In addition, note that func names should start by lowercase ; and names should be more explicit.


You could also replace Arc4random by the newest Int.random

questionNumber = Int.random(in: 0..<QB1().listY1.count)

And you should also test whether the question was already asked.

Problems with randomised array items
 
 
Q