The model layer of our application will contain the following:
- The Question structure, responsible for holding the question string, as well as the expected answer (true/false)
- The QuestionController class, responsible for loading the questions as well as advancing to the next question:
// 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()
}
}
As you notice, we have implemented our model with a controller, which helps us to perform complex logic at the model layer such as loading the questions from the network or from a local database.
The QuestionController can also be subclassed to accommodate one's needs with regard to specific questions to be loaded, different difficulty levels, and so on.