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

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!")
                Picker(selection: $selectedTimesTable, label: Text("Times table")) {
                    ForEach(2...20, id: \.self) { number in
                        Text("\(number) times table")

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

                Button("Start Game") {
                    startGame(selectedNumberOfQuestions, selectedTimesTable)

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


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



In the GameView Body your missing a point on padding


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

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


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 April 28th.

Click to save your free spot 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.