0

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
  • Did you mean to type var nextQuestion = questions[0]? Commented Jun 5, 2015 at 12:07
  • Oh yes sorry - I must not have changed it when testing! Thanks Commented Jun 5, 2015 at 12:11

2 Answers 2

1

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)")
Sign up to request clarification or add additional context in comments.

Comments

0

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
}

1 Comment

Hello, I have implemented this function into my code and it is working great. However I am struggling on the logic to create a Previous Question button. I have tried making the button minus the currentIndex by 1 and calling the nextQuestion function but it doesnt seem to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.