WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Day 35 - Times Table app crash help

Forums > 100 Days of SwiftUI

I've been stuck on this for days, and despite trying to read the code of other solutions, I can't figure out why my app crashes/hangs the simulator when I click the 'start game' button.

I'm starting to fear I have a big gap in my understanding as to what is going on with passing data between views. Sorry if this is something obvious, I hate asking for help as I feel like I should be able to figure it out by myself, but yeah, totally stuck without any obvious error messages from XCode or the simulator crash.

//
//  ContentView.swift
//  LearnTimesTables
//
//  Created by on 15/5/2023.
//

import SwiftUI

struct ContentView: View {

    struct Question {
        var question: String
        var answer: String
    }

    @State private var questionList: [Question] = []
    @State private var settingsCompleted = false

    @State private var selectedNumberOfQuestions = 5
    @State private var selectedTimesTable = 5
    @State private var userScore = 0

    var body: some View {
        if settingsCompleted {
            GameView(selectedNumberOfQuestions: selectedNumberOfQuestions, selectedTimesTable: selectedTimesTable)
        } else {
            SettingsView(startGame: startGame)
        }
    }

    func startGame(selectedNumberOfQuestions: Int, selectedTimesTable: Int ) {
        self.selectedNumberOfQuestions = selectedNumberOfQuestions
        self.selectedTimesTable = selectedTimesTable
        settingsCompleted = true
    }
}

struct GameView: View {
    let selectedNumberOfQuestions: Int
    let selectedTimesTable: Int

        var body: some View {
            Text("Game playing \(selectedNumberOfQuestions) questions")
            padding()
        }
}

struct SettingsView: View {
    @State private var selectedNumberOfQuestions = 10
    @State private var selectedTimesTable = 10

    let startGame: (Int, Int) -> Void
    init(startGame: @escaping (Int, Int) -> Void) {
        self.startGame = startGame
    }

        var body: some View {
            VStack {

                Text("Choose your times table!")
                    .font(.headline)
                Picker(selection: $selectedTimesTable, label: Text("Times table")) {
                    ForEach(2...20, id: \.self) { number in
                        Text("\(number) times table")
                    }
                }.pickerStyle(.wheel)

                Text("Choose how many questions you want!")
                    .font(.headline)
                Picker(selection: $selectedNumberOfQuestions, label: Text("Number of Questions")) {
                    Text("5 Questions").tag(5)
                    Text("10 Questions").tag(10)
                    Text("20 Questions").tag(20)
                }.pickerStyle(.segmented)

                Button("Start Game") {
                    startGame(selectedNumberOfQuestions, selectedTimesTable)
                }
                .buttonStyle(.borderedProminent)

                // For testing purposes
                Text("Selected Times Table: \(selectedTimesTable)")
                Text("Questions Quantity: \(selectedNumberOfQuestions)")

            }
        }
    }

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
//        GameView(selectedNumberOfQuestions: 5)
//        SettingsView(startGame: )
    }
}

2      

Hi,

In the GameView Body your missing a point on padding

3      

Thank you so much Hector!!! You are a life saver :-)

PS I think its strange XCode doesnt flag the incorrect .padding() syntax.

2      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, 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.