How do I access this array of structs - Swift

346 views Asked by At

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"

    }
}
}
2

There are 2 answers

1
rshankar On

A simple function to retrieve the question from the array. Declare a instance variable to hold the current index.

var currentIndex = 0

func nextQuestion() -> QuizQuestion {  
// initialize current question
var currentQuestion: QuizQuestion = QuizQuestion(question: "", answer: false, explanation: "")

if currentIndex < questions.count {
    currentQuestion =  questions[currentIndex]
}
currentIndex++

return currentQuestion
}
0
ondermerol On

You can define a nested function (the simplest form of a closure in Swift) as below:

func getNextQuestion() -> (Int) -> QuizQuestion!
{
    func incrementor(var index:Int) -> QuizQuestion! {
        if index < questions.count && index >= 0 {
            return questions[index]
        }
        else {
            return nil
        }
    }
    return incrementor
}

Then you can access your question list as below

let nextQuestion = getNextQuestion()
var question_1 = nextQuestion(0)
var question_2 = nextQuestion(1)
var question_3 = nextQuestion(2)

println("1.question text: \(question_1.question)")
println("2.question text: \(question_2.question)")
println("3.question text: \(question_3.question)")