LAST CHANCE: Save 50% on all my Swift books and bundles! >>

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      

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 July 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.