This spec is really not clear, so I'll ask a few questions.
How can I populate a list of a b c d answers being populated directly from the question array?
I want a random quesiton to populate and put radio buttons for a b c d below with answers then check the answer.
1. So, you want random question from a list of questions (the question array) ?
2. You want to display 4 answers a, b, c, d. One will be correct, the other 3 not.
3. How do you want to select those other 3? Do you predefine them ? Or do you want to select them randomly ? (however they need to have some logical link to the question).
Can I put everything inside an array and check strings?
Or is there a better method and logic that I need to comprehend?
You need first to build your data models.
I would suggest create a dictionary for Q&A, as explained in the other thread:
struct QA {
var question: String
var correctAnswer: String
var phonytAnswers: [String] = ["", "", ""] // The ones to use in buttons, along with the correct one
}
then
var allQA: [QA]
That you populate with your questions.
let qa1 = QA(question: "Who is Steve Jobs", correctAnswer: "Apple's Founder", phonytAnswers: ["Labor minister in the 80's", "Former US President", "Nobel Prize"])
let qa2 = QA(question: "What is the capital of France", answer: "Paris", phonytAnswers: ["Chicago", "Toulouse", "Lyon"])
let qa3 = QA(question: "Who was the first man on the moon", answer: "Neil Armstrong", phonytAnswers: ["Barak Obama", "Buzz Aldrin", "John Glenn"]
"Buzz Aldrin", "Jogn Glenn"])
allQA = [qa1, qa2, qa3]allQA = [qa1, qa2, qa3
So your func to get a question and its answers is:
func getQuestion() -> QA {
return allQA.randomElement()!
}
Then, you display with:
let question = getQuestion().question
you get all the answers with
let correct answer = getQuestion().correctAnswer
let phonytAnswers = getQuestion().phonytAnswers
Create an array [UIButton] to hold the 4 buttons.
Set the tags of the buttons from 0 to 3
Each time you select a question:
- select a random number let correctNum = (0...3).random
- set the title of the button number correctNum with the correct answer
- set the other titles with the phony answers
- in the IBAction (the same for all buttons), just test if tag == correctNum
- if so, answer is correct, otrherwise it is wrong.