UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 35 Challenge: how to remove an object from an array

Forums > 100 Days of SwiftUI

Hi, I have an array of all possible questions (allPossibleQuestions) and I select random questions to create an array of questions for the user to answer (questionTable). After randomly selecting a question to add to the question array, I would like to remove it from the list of all possible questions to avoid asking dupliate questions. I'm aware of ".firstIndex(of:)" but that doesn't appear to work with my array of Question objects. Any thoughts/advice would be much appreciated. Cheers, Scott.

struct Question {
    var firstNumber = 0
    var secondNumber = 0
    var questionString: String {
        return "\(firstNumber) x \(secondNumber)"
    }
    var answer: Int {
        return firstNumber * secondNumber
    }
}   

struct ContentView: View {

...

   func newGame(for timesTable: Int) {
        //reset game state
        questionTable.removeAll()

        let timesTables = [1,2]

        var allPossibleQuestions = [Question]()

        //build table of all possible questions
        for table in timesTables {

            for secondNumber in 1...12 {
                let question = Question(firstNumber: table, secondNumber: secondNumber)
                allPossibleQuestions.append(question)

            }

        }

        // build the table of random questions
        for _ in 1...numberOfQuestions {

            // randomly select the questions to add to the questions array
            if let randomQuestion = allPossibleQuestions.randomElement() {
                questionTable.append(randomQuestion)
            } 

            // once the question has been used, remove it from the array asking duplicate questions.
            if let indexNumb = allPossibleQuestions.firstIndex(of: randomQuestion) {
                allPossibleQuestions.remove(at: indexNumb)
            }
        }

3      

Instead of the last bit for _ in 1...numberOfQuestions just use allPossibleQuestions.shuffled() or allPossibleQuestions.shuffle() I can not remember which will work.

Then you just have to go through the array from 0 to ask the question

4      

.shuffle() works perfectly - thanks @NigelGee for your help and the really quick response! 🙂

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.