I have created an array called "questions" that is full of instances of the struct "QuizQuestion" - Basically I want the "nextQuestion" function to pull the next question from the array of structs (questions) and display it to the user (I understand how to present the data through labels etc, I just don`t know how to access it).
I have tried calling the array index and then the instance but that doesnt seem to work. I have also tried creating an instance of "QuizQuestion" as a variable and using that in the "NextQuestion" function which works but I don`t know how to automatically draw questions from "questions".
Thanks
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var answerLabel: UILabel!
@IBOutlet weak var explanationLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
questionVariable()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
struct QuizQuestion {
let question: String
let answer: Bool
let explanation: String
}
let questions: [QuizQuestion] = [
QuizQuestion(question: "Red is a four letter word", answer: false, explanation: "Red is not a four letter word"),
QuizQuestion(question: "Dog is a three letter word", answer: true, explanation: "Dog is most likely a three letter word"),
QuizQuestion(question: "Cow is a three letter word", answer: true, explanation: "Cow is most likely a three letter word")
]
var nextQuestion = QuizQuestion.self[1]
func questionVariable() {
if nextQuestion.answer == true {
questionLabel.text = nextQuestion.question
explanationLabel.text = nextQuestion.explanation
answerLabel.text = "Correct"
}
}
}
A simple function to retrieve the question from the array. Declare a instance variable to hold the current index.