BLACK FRIDAY: 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      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.