给你一个洗牌数组的实际例子......
在任何视图控制器的viewDidLoad() 中添加这个:
let questions:[String] = [
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
]
for _ in 1...4 {
let shuffledQuestions:[String] = questions.shuffled()
print(shuffledQuestions)
}
当您运行此程序时,您将在调试控制台中看到问题数组输出四次,每次以随机顺序输出。它看起来像这样:
["Six", "Nine", "One", "Five", "Four", "Two", "Ten", "Three", "Eight", "Seven"]
["Two", "Nine", "Seven", "Four", "Six", "Five", "Eight", "One", "Three", "Ten"]
["Nine", "Ten", "Four", "Two", "One", "Five", "Eight", "Three", "Six", "Seven"]
["Six", "Three", "Seven", "One", "Five", "Two", "Eight", "Nine", "Four", "Ten"]
当然,每次运行,顺序都会不同。
所以,这里有一个简单的 10 题真/假测验的完整示例,问题顺序是随机的(打乱的)。回答完第 10 个问题后,您可以点击“重新开始测验”按钮,您将得到相同的 10 个问题,但顺序不同:
//Model: File - Question ------------------------------------------
class Question {
var questionText : String
let answer : Bool
init(text: String, correctAnswer: Bool) {
questionText = text
answer = correctAnswer
}
}
//Model: File - QuestionBank --------------------------------------
class QuestionBank {
var list: [Question] = [
Question(text: "One is an Even number?", correctAnswer: false),
Question(text: "Two is an Even number?", correctAnswer: true),
Question(text: "Three is an Even number?", correctAnswer: false),
Question(text: "Four is an Even number?", correctAnswer: true),
Question(text: "Five is an Even number?", correctAnswer: false),
Question(text: "Six is an Even number?", correctAnswer: true),
Question(text: "Seven is an Even number?", correctAnswer: false),
Question(text: "Eight is an Even number?", correctAnswer: true),
Question(text: "Nine is an Even number?", correctAnswer: false),
Question(text: "Ten is an Even number?", correctAnswer: true),
]
}
class RandomizeQuestionsViewController: UIViewController {
let questionHeaderLabel: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .cyan
v.numberOfLines = 0
v.textAlignment = .center
return v
}()
let questionLabel: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .yellow
v.numberOfLines = 0
v.textAlignment = .center
return v
}()
let answerLabel: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .yellow
v.numberOfLines = 0
v.textAlignment = .center
return v
}()
let nextButton: UIButton = {
let v = UIButton()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .blue
v.setTitle("Next Question", for: .normal)
return v
}()
let restartButton: UIButton = {
let v = UIButton()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .red
v.setTitle("Restart Quiz", for: .normal)
return v
}()
let trueButton: UIButton = {
let v = UIButton()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
v.setTitleColor(.lightGray, for: .disabled)
v.setTitleColor(.blue, for: .normal)
v.layer.borderColor = UIColor.red.cgColor
v.setTitle("True", for: .normal)
return v
}()
let falseButton: UIButton = {
let v = UIButton()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
v.setTitleColor(.blue, for: .normal)
v.setTitleColor(.lightGray, for: .disabled)
v.layer.borderColor = UIColor.red.cgColor
v.setTitle("False", for: .normal)
return v
}()
var shuffledQuestions: [Question] = [Question]()
// arrays are zero-based
var currentQuestionIndex: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// add UI elements
view.addSubview(questionHeaderLabel)
view.addSubview(questionLabel)
view.addSubview(trueButton)
view.addSubview(falseButton)
view.addSubview(answerLabel)
view.addSubview(nextButton)
view.addSubview(restartButton)
NSLayoutConstraint.activate([
questionHeaderLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 40.0),
questionHeaderLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 40.0),
questionHeaderLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -40.0),
questionHeaderLabel.heightAnchor.constraint(equalToConstant: 30.0),
questionLabel.topAnchor.constraint(equalTo: questionHeaderLabel.bottomAnchor, constant: 0.0),
questionLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 40.0),
questionLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -40.0),
questionLabel.heightAnchor.constraint(equalToConstant: 80.0),
trueButton.topAnchor.constraint(equalTo: questionLabel.bottomAnchor, constant: 40.0),
trueButton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -60.0),
trueButton.widthAnchor.constraint(equalToConstant: 90.0),
falseButton.topAnchor.constraint(equalTo: questionLabel.bottomAnchor, constant: 40.0),
falseButton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 60.0),
falseButton.widthAnchor.constraint(equalToConstant: 90.0),
answerLabel.topAnchor.constraint(equalTo: trueButton.bottomAnchor, constant: 40.0),
answerLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 40.0),
answerLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -40.0),
answerLabel.heightAnchor.constraint(equalToConstant: 80.0),
nextButton.topAnchor.constraint(equalTo: answerLabel.bottomAnchor, constant: 40.0),
nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0.0),
nextButton.widthAnchor.constraint(equalToConstant: 160.0),
restartButton.topAnchor.constraint(equalTo: answerLabel.bottomAnchor, constant: 40.0),
restartButton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0.0),
restartButton.widthAnchor.constraint(equalToConstant: 160.0),
])
trueButton.addTarget(self, action: #selector(trueTapped(_:)), for: .touchUpInside)
falseButton.addTarget(self, action: #selector(falseTapped(_:)), for: .touchUpInside)
nextButton.addTarget(self, action: #selector(nextQuestionTapped(_:)), for: .touchUpInside)
restartButton.addTarget(self, action: #selector(restartQuizTapped(_:)), for: .touchUpInside)
restartQuizTapped(nil)
}
@objc func restartQuizTapped(_ sender: Any?) -> Void {
// hide restart button
restartButton.isHidden = true
// nextQuestion func increments the index...
// set it to -1 so the first question will be index 0
currentQuestionIndex = -1
// shuffle the questions
shuffledQuestions = QuestionBank().list.shuffled()
// show the question
nextQuestionTapped(nil)
}
@objc func nextQuestionTapped(_ sender: Any?) -> Void {
// hide next button
nextButton.isHidden = true
// reset true/false button borders
trueButton.layer.borderWidth = 0
falseButton.layer.borderWidth = 0
// increment the index
currentQuestionIndex += 1
if currentQuestionIndex < shuffledQuestions.count {
// get current Question object from shuffled array
let q: Question = shuffledQuestions[currentQuestionIndex]
// set the label texts
questionHeaderLabel.text = "Question \(currentQuestionIndex + 1) of \(shuffledQuestions.count)"
questionLabel.text = q.questionText
answerLabel.text = "Select True or False"
// enable true/false buttons
trueButton.isEnabled = true
falseButton.isEnabled = true
} else {
// out of questions, so show restart button
restartButton.isHidden = false
}
}
@objc func trueTapped(_ sender: Any?) -> Void {
// highlight selected button
trueButton.layer.borderWidth = 3
// get current Question object from shuffled array
let q: Question = shuffledQuestions[currentQuestionIndex]
var answerText = ""
if q.answer == true {
answerText = "Correct!" + "\n" + "It IS an Even number!"
} else {
answerText = "Wrong!" + "\n" + "It is NOT an Even number!"
}
updateUI(feedback: answerText)
}
@objc func falseTapped(_ sender: Any?) -> Void {
// highlight selected button
falseButton.layer.borderWidth = 3
// get current Question object from shuffled array
let q: Question = shuffledQuestions[currentQuestionIndex]
var answerText = ""
if q.answer == false {
answerText = "Correct!" + "\n" + "It is NOT an Even number!"
} else {
answerText = "Wrong!" + "\n" + "It IS an Even number!"
}
updateUI(feedback: answerText)
}
func updateUI(feedback answer: String) -> Void {
answerLabel.text = answer
// disable true/false buttons
trueButton.isEnabled = false
falseButton.isEnabled = false
// if there are more questions
if currentQuestionIndex < shuffledQuestions.count - 1 {
// show next question button
nextButton.isHidden = false
} else {
// show restart button
restartButton.isHidden = false
}
}
}
这都是基于代码的,所以没有@IBOutlets 或@IBActions ...只需从一个新的视图控制器开始,并将其类分配给RandomizeQuestionsViewController。