2017-07-19 35 views
0

我正在为iOS开发一个测验应用程序,并将问题保存在一个数组中,并将答案保存在另一个数组中。到目前为止,我已经能够用arc4random从问题数组中获得一个随机问题。但是,当我尝试设置按钮以显示随机答案选项时,它们与指定的问题不匹配。例如,question数组中index = 0处的问题与answers数组中index = 0处的answer数组一致,因此。如何随时随地产生正确的答案(换句话说,我如何确保它们都始终具有由arc4random方法或任何方法拉取的匹配索引)?如何将对与数组连接以包含相同的索引?

下面是我的一些代码:

//random question generation function 
func randomQuestion() { 
    index = Int(arc4random_uniform(UInt32(questions.count))) 
    questionLabel.text = questions[index] 

let questions = ["Who is Thor's half brother?", "What is the name of Thor's hammer?", "What is Thor's father name?"] 

//Each correct answer is placed at index 0 (right answers are Atum, Mjolinr, & Odin) 
var answers = [["Atum", "Loki", "Red Norvell", "Kevin Masterson"], ["Mjolinr", "Uru", "Stormbreaker", "Odin's Staff"], ["Odin, "Sif", "Heimdall", "Balder"]] 


//This variable is used in a later function that helps randomize wear the correct answer will be placed randomly from a set of buttons 
var rightAnswerBox:UInt32 = 0 


IBAction func buttonAction(_ sender: AnyObject) { 

if (sender.tag == Int(rightAnswerBox)) { 



print ("Correct!") 
} 

else { 

    wrongSeg() 
print ("Wrong!") 

    } 

    if (currentQuestion != questions.count) 
    { 
     newQuestion() 
    } 
} 



override func viewDidAppear(_ animated: Bool) 
{ 
newQuestion() 


} 




//function that displays new question 
func newQuestion() 
{ 

//countdown timer section 




randomQuestion() 

rightAnswerBox = arc4random_uniform(4)+1 

//create a button 
var button:UIButton = UIButton() 

var x = 1 

for index in 1...4 
{ 
    //creat a button 
    button = view.viewWithTag(index) as! UIButton 

    if (index == Int(rightAnswerBox)) 
    { 
     button.setTitle(answers[currentQuestion][0], for: .normal) 



    } 

    else { 




     button.setTitle(answers[currentQuestion][x], for: .normal) 

    x += 1 
     } 
} 


currentQuestion += 1 

}

+3

而不是数组答案,你可以把问题的词典作为关键和答案作为价值! –

+0

你可以随机化一本字典吗? –

+0

我还没试过。但我瘦,你可以随机排列字典键。 –

回答

3

使用Dictionary分配的答案的适当的问题。

let QADictionary = ["Who is Thor's half brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"],"What is the name of Thor's hammer?": ["Mjolinr", "Uru", "Stormbreaker", "Odin's Staff"], "What is Thor's father name?" : ["Odin", "Sif", "Heimdall", "Balder"] ] 


//Get question list 
let questionsList = Array(QADictionary.keys) 
// Random question index 
let index = Int(arc4random_uniform(UInt32(questionsList.count))) 
// Fetch question from list 
let question = questionsList[index] 
// Assign to label 
questionLabel.text = question 

//Get the answer for the randomly chosen question. Because you question is the `Key` for the dictionary. 
let answer = QADictionary[question] 
+0

有没有办法分别访问密钥的每个值?就像我想单独设置每个答案值到一个按钮一样? (所以我会有四个按钮,每个显示与问号键配对的答案值) –

+0

@CaptainCode'let answer = QADictionary [question]'会给你答案数组。迭代数组以获取问题的值。 'questionLabel.text'有你的问题,那就是你的字典的关键。 – Subramanian

+0

您可以通过使用我作为示例给出的代码来举个例子吗?我仍然困惑于我将如何访问每个键(问题)的每个值(答案) –

2

@Saurabh Prajapati的建议是正确的为您的要求。

Step1:首先将您的问题和解答存储在字典中。

第2步:使用字典的关键字组成数组,如下所示。

例如,你的字典名称是quizDic。

var questionsArray = quizDic.keys.Array 

第三步:找到你questionsArray数和值传递给随机生成的函数。

第4步:然后根据从questionsArray中随机选择的密钥,从quizDic访问您正确的答案。

相关问题