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

SOLVED: Question about using an Array based on a Struct - Milestone Project 4-6, Day 35

Forums > 100 Days of SwiftUI

Working through the Milestone Project 4-6 with the multiplication tables game - I'm using a Struct to store information about all the possible questions and answers. The multiplication tables and answers are then stored in an Array with all possible questions and answers.

I'm having trouble understanding how to then either print out an individual question from the array with all the questions, or how to pull a random set of questions into a different array which then could be used to show each individual question.

My thought was that Text(allQuestions[2].questionString) would have printed out the question string in the second spot of the full array, but I keep getting errors.

Many thanks for any suggestions!

struct Question: Identifiable {
    var id = UUID()
    var firstNum: Int
    var secondNum: Int
    var questionString: String {
        "\(firstNum) X \(secondNum)"
    }
    var answer: Int {
         firstNum * secondNum
    }
}

struct ContentView: View {

    @State var difficulty: Int = 5
    @State private var questionAmount: Int = 10

    @State var allQuestions: [Question] = []

    @State private var playerAnswer: String = "0"

    @State private var questionNum: Int = 1

    @State private var showingSettings = false
    @State private var showingResults = false

    var body: some View {
        NavigationView {
            VStack {
                Spacer()
                Text("This is Question Number: \(questionNum)")

                Text("Player Answer is \(playerAnswer)")

                Text(allQuestions[2].questionString)

                Spacer()
            }
        }
        .onAppear(perform: createAllQuestions)
        .sheet(isPresented: $showingSettings) {
            SettingsView(difficulty: $difficulty, questionAmount: $questionAmount)
        }
        .sheet(isPresented: $showingResults) {
            ResultsView()
        }
    }

    func createAllQuestions() {
        for firstNum in (1...12) {
            for secondNum in 1...12 {
                allQuestions.append(Question(firstNum: firstNum, secondNum: secondNum))
            }
        }

2      

First, you do not need / want the question array to be declared as @State. This puts a wrapper around the parameter so that the View can remember the 'state' when the View is updated, or re-displayed. You should just have the array of questions.

Removing the @State will cause two issues. The func 'createAllQuestions()' will have to become a mutating func. This is turn means that you cannot use the .onAppear. It will have to be replaced by an init.

Remove the @State

var allQuestions: [Question] = []

Put this somewhere in your ContentView struct, say just before the var body: some View {

init() {
    createAllQuestions()
}

comment out or remove the .onAppear

// .onAppear(perform: createAllQuestions)

Change the func to a mutating func

mutating func createAllQuestions() {
    for firstNum in (1...12) {
        for secondNum in 1...12 {
            allQuestions.append(Question(firstNum: firstNum, secondNum: secondNum))
        }
    }
}

2      

Thanks so much! Huge help for sure.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.