Now that we have the model layer properly defined, let's take a look at the view layer. From here, we'll need to somehow display the current question.
We'll have a QuestionView, responsible for printing the question in the console, but it could also be responsible for a label on-screen in another scenario. We'll also have a PromptView, which will be used to display the prompt for the user to answer.
Finally, we'll have a ViewController, responsible for providing a simple interface that properly displays every question and prompt on-screen, as well as responding to the user on whether their answer was correct:
struct QuestionView {
func show(question: Question) {
print(question.question)
}
}
struct PromptView {
func show() {
print("> ", terminator: "")
}
}
class ViewController {
private let questionView = QuestionView()
private let promptView = PromptView()
func ask(question: Question) {
questionView.show(question: question)
promptView.show()
}
func goodAnswer() { /* implement me */ }
func badAnswer() { /* implement me */ }
func finishPlaying() { /* implement me */ }
}
Again we have a controller in our view layer, which is what's expected. A ViewController is a general controller, responsible for orchestrating views inside the view layer, which is its only responsibility.
We have no logic around prompting the user, reading their input, and so on. Remember that we're in the view layer, and the view layer's responsibility is to display information, not to gather input. For that, let's look at the controller layer implementation.