I want to display a String in a separate function and just call the function when the user choose the wrong answer, the problem is when I create the function and I try to use it my app crash and is telling me an error about the index is out of range..... any suggestion how can I fix this? or a recommendation to do a better and clean job? Here is my code:
//This is my struct
struct Question {
var Question: String!
var Answers: [String]!
var Answer: Int!
var Img: UIImage!
var Info: String!
}
override func viewDidLoad() {
super.viewDidLoad()
//Here the way I display the questions
questions = [
Question(
Question: "Question #1",
Answers: ["A","B","C","D"],
Answer: 1,
Img: UIImage.self(named: "steve"),
Info: "Steve"
),
]
//Here is my function that I want to create to display the Info:
private function showInformation() {
infoLabel.text = questions[Question].Info
}
Ps: If need more details let me know, by the way my function to create a random question is this
private func pickingRandomQuestion() {
if questions.count > 0 {
questionNumber = random() % questions.count //This make a random pick of Question
questionLabel.text = questions[questionNumber].Question //Converting quesitonLabel into TEXT
answerNumber = questions[questionNumber].Answer
imgDisplay.image = questions[questionNumber].Img
//Im trying to use one of this examples to display but is not working :(
answerA.setTitle(questions[questionNumber].Answers[0], forState: .Normal)
answerB.setTitle(questions[questionNumber].Answers[1], forState: .Normal)
answerC.setTitle(questions[questionNumber].Answers[2], forState: .Normal)
answerD.setTitle(questions[questionNumber].Answers[3], forState: .Normal)
questions.removeAtIndex(questionNumber)
} else {
finishGame.hidden = false
answerA.hidden = true
answerB.hidden = true
answerC.hidden = true
answerD.hidden = true
}
}