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

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      

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.