The model layer still contains the Question structure and the QuestionController class. We can keep those components as they are. The ViewModel pattern is used to bind the model layer to the view layer, and its responsibility is to abstract the details of rendering the model into a view:
// Question.swift
enum BooleanAnswer: String {
case `true`
case `false`
}
struct Question {
let question: String
let answer: BooleanAnswer
}
extension Question {
func isGoodAnswer(result: String?) -> Bool {
return result == answer.rawValue
}
}
// QuestionController.swift
class QuestionController {
private var questions = [Question]()
// Load the questions from memory or disk
func load() { /* load from disk, memory or else */ }
// Get the next question, if available
func next() -> Question? {
return questions.popLast()
}
}
Unlike with the MVC design pattern, where we want to design our views before tying everything up with the controllers, we won't jump right into the View layer. With this pattern, it is more natural to write the ViewModel first.