NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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 {
                Text("This is Question Number: \(questionNum)")

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


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

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


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() {

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))


Thanks so much! Huge help for sure.


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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.