我正在为一个iOS问答应用程序编写代码,到目前为止,这段代码包含了一个字典,它将问题作为关键,并根据问题的价值来回答这个问题。我的代码将一个键(问题)数组随机化,并选择一个要随机显示的键。我还将值(答案)放在一个值数组中,并将它们随机化,并确保选择了问题数组的任何索引,并选择了相同的答案数组索引。
但是,当我运行模拟器时,问题标签文本保持不变,没有从要选择的问题数组中选择一个新的索引作为新的显示问题,但另一方面,在我选择答案之后,要更改的答案数组。
简单地说,问题标签文本始终保持不变,按钮的标题(值数组)确实选择了不同的索引。,我如何才能到达问题标签更改而不保持不变的地方?
这是我的代码:
class QuestionDetails {
let QADictionary = [
"Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"],
"What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"],
"Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]
]
//get question list
func questionWithAnswers() {
let listQuestions = QuestionDetails()
var questionList = Array(listQuestions.QADictionary.keys)
//random question index
var rand = Int(arc4random_uniform(UInt32(questionList.count)))
let randAnswers = rand
//random answer index
var answerList = Array(listQuestions.QADictionary.values)
var choices = answerList[randAnswers]
//fetch questions from list
var question = questionList[rand]
questionLabel.text = question
//function for new question and button titles
rightAnswerBox = arc4random_uniform(4)+1
//create button
var button:UIButton = UIButton()
var x = 1
for index in 1...4
{
button = view.viewWithTag(index) as! UIButton
if (index == Int(rightAnswerBox))
{
button.setTitle(choices[0], for: .normal)
}
else {
button.setTitle(choices[x], for: .normal)
x += 1
}
randomImage()
}
}
//variables
var currentQuestion = 0
var rightAnswerBox:UInt32 = 0
var index = 0
//Question Label
@IBOutlet weak var questionLabel: UILabel!
//Answer Button
@IBAction func buttonAction(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerBox)) {
//removes asked question
print ("Correct!")
}
else {
wrongSeg()
print ("Wrong!")
}
}
override func viewDidAppear(_ animated: Bool) {
questionWithAnswers()
}
}发布于 2017-07-22 01:12:12
以字典的keys为例。强迫列队。以数组的count为例。在0和count之间生成一个随机数,这是不包含的.将随机数降到Int。获取该索引处的数组元素。把它当作你的钥匙。
对上述方法的检验:
extension ClosedRange where Bound : FloatingPoint {
public func random() -> Bound {
let range = self.upperBound - self.lowerBound
let randomValue = (Bound(arc4random_uniform(UINT32_MAX)) / Bound(UINT32_MAX)) * range + self.lowerBound
return randomValue
}
}
let QADictionary = [
"Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"],
"What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"],
"Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]
]
func test () {
let questions = Array(QADictionary.keys)
let r = (0...Double(questions.count)).random()
let randomQuestion = questions[Int(r)]
print(randomQuestion)
}
(1...40).forEach {_ in test()}
/*
Who is Thor's brother?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is the father of Thor?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is Thor's brother?
Who is the father of Thor?
What is the name of Thor's hammer?
Who is the father of Thor?
What is the name of Thor's hammer?
Who is the father of Thor?
What is the name of Thor's hammer?
Who is the father of Thor?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
Who is the father of Thor?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
Who is Thor's brother?
What is the name of Thor's hammer?
What is the name of Thor's hammer?
*/https://stackoverflow.com/questions/45249499
复制相似问题