Advertisement
Don_Mag

General Quiz Logic

Nov 23rd, 2022
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. Assuming you have a "database" of 50+ questions - either in a file, or using Core Data, or getting them from a remote server, etc...
  2.  
  3. Supposing:
  4.  
  5. - total number of questions is 50
  6. - number of questions to show is 15
  7.  
  8. Let's say we have a Question Data Object:
  9.  
  10. struct Question {
  11. var title: String = ""
  12. var answers: [String] = []
  13. var description: String = ""
  14. var userAnswered: Int = 0
  15. }
  16.  
  17. and we have an array of Questions:
  18.  
  19. var questions: [Question] = []
  20.  
  21. On launch:
  22.  
  23. - shuffle an array of 0..<50 Ints
  24. - we now have, for example, [23, 25, 4, 18, 44, 42, 41, ...]
  25. - loop through that array
  26. - load question shuffledInts[i]
  27. - create a Question Data object
  28. - fill the object with the question data, shuffling the answers string array
  29. - append that Question to questions array
  30.  
  31. We now have an Array of random Question objects, with their answers shuffled.
  32.  
  33. Proceed with the Quiz, using that array for each successive question.
  34.  
  35. When user selects an answer, update the Question object with the user's answer.
  36.  
  37. When the Quiz is finished, use a TableView to display all of those Questions, along with showing the user's answer and the Description.
  38.  
  39. 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.
  40.  
  41. Then, change "On launch" to:
  42.  
  43. - if saved file exists
  44. - load saved file and continue with Quiz
  45. - else
  46. - shuffle an array of 0..<50 Ints
  47. - etc...
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement