Thanks for the advices guys. But I found the way on how to do it. I only changed the pickQuestion function.
func pickQuestion ()
{
if Questions.count > 0 && questionscount < 8{
QNumber = Int(arc4random_uniform(UInt32(Questions.filter{$0.Question.hasPrefix("KEK")}.count)))
questionscount += 1
questionLabel.text = Questions.filter{$0.Question.hasPrefix("KEK")}[QNumber].Question
self.title = "Ερώτηση: \(Int(questionscount))/50"
answerNumber = Questions[QNumber].Answer
for i in 0..<buttons.count{
buttons[i].setTitle(Questions[QNumber].Answers[i], for: UIControlState.normal)
}
print(QNumber)
Questions.remove(at: QNumber)
}else if Questions.count > 0 && questionscount < 13{
QNumber = Int(arc4random_uniform(UInt32(Questions.filter{$0.Question.hasPrefix("M")}.count)))
questionscount += 1
questionLabel.text = Questions.filter{$0.Question.hasPrefix("M")}[QNumber].Question
self.title = "Ερώτηση: \(Int(questionscount))/50"
answerNumber = Questions.filter{$0.Question.hasPrefix("M")}[QNumber].Answer
for i in 0..<buttons.count{
buttons[i].setTitle(Questions.filter{$0.Question.hasPrefix("M")}[QNumber].Answers[i], for: UIControlState.normal)
}
print(QNumber)
Questions.remove(at: QNumber)
}else if Questions.count > 0 && questionscount < 18{
QNumber = Int(arc4random_uniform(UInt32(Questions.filter{$0.Question.hasPrefix("N")}.count)))
questionscount += 1
questionLabel.text = Questions.filter{$0.Question.hasPrefix("N")}[QNumber].Question
self.title = "Ερώτηση: \(Int(questionscount))/50"
answerNumber = Questions.filter{$0.Question.hasPrefix("N")}[QNumber].Answer
for i in 0..<buttons.count{
buttons[i].setTitle(Questions.filter{$0.Question.hasPrefix("N")}[QNumber].Answers[i], for: UIControlState.normal)
}
Questions.remove(at: QNumber)
}else if Questions.count > 0 && questionscount < 24{
QNumber = Int(arc4random_uniform(UInt32(Questions.filter{$0.Question.hasPrefix("A")}.count)))
questionscount += 1
questionLabel.text = Questions.filter{$0.Question.hasPrefix("A")}[QNumber].Question
self.title = "Ερώτηση: \(Int(questionscount))/50"
answerNumber = Questions.filter{$0.Question.hasPrefix("A")}[QNumber].Answer
for i in 0..<buttons.count{
buttons[i].setTitle(Questions.filter{$0.Question.hasPrefix("A")}[QNumber].Answers[i], for: UIControlState.normal)
}
Questions.remove(at: QNumber)
}else if Questions.count > 0 && questionscount < 30{
QNumber = Int(arc4random_uniform(UInt32(Questions.filter{$0.Question.hasPrefix("ΑΔ")}.count)))
questionscount += 1
questionLabel.text = Questions.filter{$0.Question.hasPrefix("ΑΔ")}[QNumber].Question
self.title = "Ερώτηση: \(Int(questionscount))/50"
answerNumber = Questions.filter{$0.Question.hasPrefix("ΑΔ")}[QNumber].Answer
for i in 0..<buttons.count{
buttons[i].setTitle(Questions.filter{$0.Question.hasPrefix("ΑΔ")}[QNumber].Answers[i], for: UIControlState.normal)
}
Questions.remove(at: QNumber)
}else if Questions.count > 0 && questionscount < 35{
QNumber = Int(arc4random_uniform(UInt32(Questions.filter{$0.Question.hasPrefix("ΕΠ")}.count)))
questionscount += 1
questionLabel.text = Questions.filter{$0.Question.hasPrefix("ΕΠ")}[QNumber].Question
self.title = "Ερώτηση: \(Int(questionscount))/50"
answerNumber = Questions.filter{$0.Question.hasPrefix("ΕΠ")}[QNumber].Answer
for i in 0..<buttons.count{
buttons[i].setTitle(Questions.filter{$0.Question.hasPrefix("ΕΠ")}[QNumber].Answers[i], for: UIControlState.normal)
}
Questions.remove(at: QNumber)
}else if Questions.count > 0 && questionscount < 44{
QNumber = Int(arc4random_uniform(UInt32(Questions.filter{$0.Question.hasPrefix("T")}.count)))
questionscount += 1
questionLabel.text = Questions.filter{$0.Question.hasPrefix("T")}[QNumber].Question
self.title = "Ερώτηση: \(Int(questionscount))/50"
answerNumber = Questions.filter{$0.Question.hasPrefix("T")}[QNumber].Answer
for i in 0..<buttons.count{
buttons[i].setTitle(Questions.filter{$0.Question.hasPrefix("T")}[QNumber].Answers[i], for: UIControlState.normal)
}
Questions.remove(at: QNumber)
}else if Questions.count > 0 && questionscount < 50{
QNumber = Int(arc4random_uniform(UInt32(Questions.filter{$0.Question.hasPrefix("ΑΝΘ")}.count)))
questionscount += 1
questionLabel.text = Questions.filter{$0.Question.hasPrefix("ΑΝΘ")}[QNumber].Question
self.title = "Ερώτηση: \(Int(questionscount))/50"
answerNumber = Questions.filter{$0.Question.hasPrefix("ΑΝΘ")}[QNumber].Answer
for i in 0..<buttons.count{
buttons[i].setTitle(Questions.filter{$0.Question.hasPrefix("ΑΝΘ")}[QNumber].Answers[i], for: UIControlState.normal)
}
Questions.remove(at: QNumber)
}else
{
let alert = UIAlertController(title: "Σκόρ", message: "Απάντησες σωστά τις \(Int(score)) από τις \(Int(questionscount)) ερωτήσεις! \n \(String(format: "%.0f",(score/questionscount*100))) %", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Μενού", style: UIAlertActionStyle.default, handler: { action in
self.navigationController?.popToRootViewController(animated: true)
}))
self.present(alert, animated: true, completion: nil)
}
Hide()
}
1
solved Quiz app to pick questions from 8 group of questions randomly?