Ios 如何将该对连接到数组以包含相同的索引?

Ios 如何将该对连接到数组以包含相同的索引?,ios,arrays,swift,xcode,indexing,Ios,Arrays,Swift,Xcode,Indexing,我正在为iOS开发一个测验应用程序,我将问题保存在一个数组中,将答案保存在另一个数组中。到目前为止,我已经能够使用arc4random让标签显示问题数组中的随机问题。但是,当我尝试设置按钮以显示随机答案选项时,它们与指定的问题不匹配。例如,问题数组中索引为0的问题与答案数组中索引为0的答案数组相匹配,依此类推。我如何确保生成随机问题时正确答案的显示位置(换句话说,我如何确保它们始终具有由arc4random方法或任何方法提取的匹配索引) 以下是我的一些代码: //random question

我正在为iOS开发一个测验应用程序,我将问题保存在一个数组中,将答案保存在另一个数组中。到目前为止,我已经能够使用arc4random让标签显示问题数组中的随机问题。但是,当我尝试设置按钮以显示随机答案选项时,它们与指定的问题不匹配。例如,问题数组中索引为0的问题与答案数组中索引为0的答案数组相匹配,依此类推。我如何确保生成随机问题时正确答案的显示位置(换句话说,我如何确保它们始终具有由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

}

使用
词典
为适当的问题指定答案

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]

使用
词典
为相应的问题指定答案

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]

@Saurabh Prajapati的建议符合您的要求

步骤1:首先将问题和答案存储在字典中

第2步:使用字典的键创建一个数组,如下所示

例如,你的字典名是quizDic

 var questionsArray = quizDic.keys.Array
第三步:找到您的问题ray count并将该值传递给随机生成的函数


第4步:然后根据随机选择的问题答案,从quizDic中获取正确答案。

@Saurabh Prajapati的建议符合您的要求

步骤1:首先将问题和答案存储在字典中

第2步:使用字典的键创建一个数组,如下所示

例如,你的字典名是quizDic

 var questionsArray = quizDic.keys.Array
第三步:找到您的问题ray count并将该值传递给随机生成的函数


第4步:然后根据问题数组中随机选择的键从quizDic中访问正确答案。

您可以使用以问题为键、以答案为值的字典,而不是答案数组!你能把字典随机化吗?我还没试过。但我认为你们可以随机排列字典键的数组,而不是答案的数组,你们可以用带问题的字典作为键,答案作为值!你能把字典随机化吗?我还没试过。但我想你可以随机排列字典键。有没有办法单独访问键的每个值?比如,如果我想单独将每个答案值设置为一个按钮?(因此我有四个按钮,每个按钮都显示一个与问题键配对的答案值)@CaptainCode
let answer=qadctionary[question]
将为您提供答案数组。迭代数组以获取问题的值。还有
questionLabel.text
有你的问题,这是你字典的关键。你能用我的代码举例吗?如果let-answer=qadctionary[question]{let-option1=answer[0]let-option2=answer[1]let-option3=answer[2]},我仍然不知道如何访问每个键(question)@CaptainCode
的每个值(答案)
谢谢!这很有意义有没有一种方法可以单独访问密钥的每个值?比如,如果我想单独将每个答案值设置为一个按钮?(因此我有四个按钮,每个按钮都显示一个与问题键配对的答案值)@CaptainCode
let answer=qadctionary[question]
将为您提供答案数组。迭代数组以获取问题的值。还有
questionLabel.text
有你的问题,这是你字典的关键。你能用我的代码举例吗?如果let-answer=qadctionary[question]{let-option1=answer[0]let-option2=answer[1]let-option3=answer[2]},我仍然不知道如何访问每个键(question)@CaptainCode
的每个值(答案)
谢谢!这是有道理的