Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Assuming you have a "database" of 50+ questions - either in a file, or using Core Data, or getting them from a remote server, etc...
- Supposing:
- - total number of questions is 50
- - number of questions to show is 15
- Let's say we have a Question Data Object:
- struct Question {
- var title: String = ""
- var answers: [String] = []
- var description: String = ""
- var userAnswered: Int = 0
- }
- and we have an array of Questions:
- var questions: [Question] = []
- On launch:
- - shuffle an array of 0..<50 Ints
- - we now have, for example, [23, 25, 4, 18, 44, 42, 41, ...]
- - loop through that array
- - load question shuffledInts[i]
- - create a Question Data object
- - fill the object with the question data, shuffling the answers string array
- - append that Question to questions array
- We now have an Array of random Question objects, with their answers shuffled.
- Proceed with the Quiz, using that array for each successive question.
- When user selects an answer, update the Question object with the user's answer.
- When the Quiz is finished, use a TableView to display all of those Questions, along with showing the user's answer and the Description.
- If you want to allow the user to "Save Progress," you'll also need to Save the questions array to local storage (documents folder, for example). When the user completes a Quiz, don't save the data to a file and delete the data file if it exists.
- Then, change "On launch" to:
- - if saved file exists
- - load saved file and continue with Quiz
- - else
- - shuffle an array of 0..<50 Ints
- - etc...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement