Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7199

SwiftUI app keeps crashing probably due to ForEach

$
0
0

I was trying to create a Tic-Tac-Toe game using SwiftUI, but Simulator crashes whenever I try to get the code to run. Here's my code:

import SwiftUIstruct ContentView: View {    @State private var allMarks: Array? = [nil, nil, nil, nil, nil, nil, nil, nil, nil]    var body: some View {        HStack(content: {            ForEach(1..<3) { i in                VStack(content: {                    ForEach(1..<3) { i in                        ZStack(content: {                            RoundedRectangle(cornerRadius: 12.0, style: .continuous)                                .aspectRatio(contentMode: .fit)                                .foregroundColor(Color(UIColor.systemGroupedBackground))                                .onTapGesture {                                    if allMarks?[i] == nil {                                        allMarks?[i] = "circle"                                        var randomCell = allMarks?.randomElement()                                        repeat {                                            randomCell = allMarks?.randomElement()                                        } while randomCell == nil                                        randomCell = "xmark"                                    }                                }                            Image(systemName: allMarks?[i] as! String)                        })                    }                })            }        })    }}struct ContentView_Previews: PreviewProvider {    static var previews: some View {        ContentView()    }}

I tried removing ForEach and pasting the contents of the ZStack other two times and then pasting said ZStack again twice and it didn't crash, so I assume it's ForEach that caused the problem. I'm not sure thought as it crashed a few times, too, even after I completely removed ForEach. Can anyone help me figure out what's wrong and what I could do to fix it?


Viewing all articles
Browse latest Browse all 7199

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>