This question already has an answer here:
- How do I shuffle an array in Swift? 21 answers
Very new to Swift and rusty developing in general
I have a questionnaire style app, where users can respond to questions, I want to keep track of the correct and incorrect responses as they answer.
I have it working for a known number of questions, but want to build an array of random questions from the base 9 possible responses.
TLDR can you build a randomised array of questions from a class?
class Question {
let questionSymbol : String
let answer : Int
init(text: String, correctAnswer: Int){
questionSymbol = text
answer = correctAnswer
}
}
class QuestionBank {
var list = [Question]()
init() {
let item = Question(text : "\u{2265}",correctAnswer : 1)
list.append(item)
list.append(Question(text : "\u{00B1}", correctAnswer : 2 ))
list.append(Question(text : "\u{226A}", correctAnswer : 3 ))
list.append(Question(text : "\u{03A0}", correctAnswer : 4 ))
list.append(Question(text : "\u{0436}", correctAnswer : 5 ))
list.append(Question(text : "\u{03C8}", correctAnswer : 6 ))
list.append(Question(text : "\u{2206}", correctAnswer : 7 ))
list.append(Question(text : "\u{26AC}", correctAnswer : 8 ))
list.append(Question(text : "\u{2191}", correctAnswer : 9 ))
}
}
I have looked and seen a number of randomisation things arc4random etc, but I am not sure how to build an array of say 150 random questions preserving the above structure in an array type based on this...!
Aucun commentaire:
Enregistrer un commentaire