dimanche 20 mai 2018

Swift 4 - Fatal error: Index out of range

I need help for finalize my code in Swift 4: I want to shuffle my questions array, but it always crashes after one time and shows this message:

 "Fatal error: Index out of range".

Here is my code:

class ThirdViewController: UIViewController {

var questions = ["A", "B", "C", "D", "E"]
var answers = [["1","2","3"],["2","1","3"],["3","2","1"],["1","3","2"],["2","3","1"]]

// Variables
var rightAnswerPlacement:UInt32 = 0
var shuffled = [String]();
let randomNumber = Int(arc4random() % 5)

// Label Question
@IBOutlet weak var Label: UILabel!

// Buttons
@IBAction func Button(_ sender: UIButton) {

    if (sender.tag == Int(rightAnswerPlacement)) {
        print ("RIGHT")
        newQuestion()
    }
    else {
        print ("WRONG")
        newQuestion()
    }
}

override func viewDidAppear(_ animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true
    newQuestion()
}

// Functions
func newQuestion() {

    Label.text = questions[randomNumber]   // ----------> Fatal error: Index out of range !!! -------------------------------------------------------------
    rightAnswerPlacement = arc4random_uniform(3)+1

    for _ in 0..<questions.count {
        let randomNumber = Int(arc4random_uniform(UInt32(questions.count)))
        shuffled.append(questions[randomNumber])
        questions.remove(at: randomNumber)
    }

// Create a Button
var Button:UIButton = UIButton()
var x = 1
for i in 1...3 {
    Button = view.viewWithTag(i) as! UIButton
    if (i == Int(rightAnswerPlacement)) {
        Button.setTitle(answers[randomNumber][0], for: .normal)
    }
    else {
        Button.setTitle(answers[randomNumber][x], for: .normal)
        x = 2
    }
}
}

It seems there is a problem with my variable randomNumber but I don't understand how to fix it. I have seen similar questions on the forum but no answer which resolves my problem. Could someone help me please? Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire