简体   繁体   中英

How to permanently remove item from array after it's index is called?

My following code displays a question and its matching answer by making an array of keys and values from a dictionary and making sure both arrays have the same index that is pulled. However after a question and its answers are randomly selected I want to remove them permanently from their respective arrays until all the other items in the array have been displayed. Simply put, how can I make sure a question and set of answers are not shown again until all the items are shown or until the user gets an answer wrong? As of now my code only makes sure the same random index isn't chosen twice in a row when I want to make sure it isn't shown twice at all.

Here is my code (swift for iOS):

func randomQuestion() {


    //random question
    var questionList = Array(QADictionary.keys)
    var rand = Int(arc4random_uniform(UInt32(questionList.count)))
    questionLabel.text = questionList[rand]




    //matching answers
    var answerList = Array(QADictionary.values)
    var choices = answerList[rand]




        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

            }

            func removePair() {
                questionList.remove(at: rand)
                answerList.remove(at: rand)


            }


            randomImage()

        }
    }


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"]]

Move your questionList to a property of the viewController so that it stays around from one call of randomQuestion to the next. Reload it only when it is empty.

Use the dictionary to find the matching answers instead of using a second array.

var questionList = [String]()

func randomQuestion() {

    //if no more questions, reload the questionList
    if questionList.isEmpty {
        questionList = Array(QADictionary.keys)
    }

    var rand = Int(arc4random_uniform(UInt32(questionList.count)))
    questionLabel.text = questionList[rand]

    //matching answers
    var choices = QADictionary[questionList[rand]]!

    questionList.remove(at: rand)

    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

        }

        func removePair() {
            questionList.remove(at: rand)
        }

        randomImage()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM