I am running into a crash issue with what seems like a super simple project. This all works in Preview up until the home button is pressed in the Info view. It flashes back to the Colors list, then crashes. Interestingly, it works in the Similar with no issues. This is on an M2 Pro MacBook Pro running Ventura/Xcode 13.3.1. I haven’t upgraded yet because I have read enough horror stories about Sonoma, especially with respect to Parallels Desktop, which I need to keep running for a little longer.
My question is, is this a problem in my code that I am missing, or a bug that I just need to work around and accept for now? And, is there anything in the crash dump that might be helpful? I have looked through it and I can’t find anything that defines WHY I got a crash, but it might as well be in Klingon as far as my comprehension of what I am reading. 🙂
AppEntry.swift
import SwiftUI
@main
struct AppEntry: App {
@StateObject var router = Router()
var body: some Scene {
WindowGroup {
NavigationStack(path: $router.path) {
ContentView()
}
.environmentObject(router)
}
}
}
class Router: ObservableObject {
@Published var path:NavigationPath = NavigationPath()
func back() {
path.removeLast()
}
func reset() {
path = NavigationPath()
}
}
ContentView.swift
import SwiftUI
struct ContentView: View {
@EnvironmentObject var router: Router
private var colors: [Color] = [.indigo, .yellow, .green, .orange, .brown]
var body: some View {
List(colors, id: .self) { color in
NavigationLink(color.description, value: color)
}
.listStyle(.plain)
.navigationTitle("Colors")
.navigationDestination(for: Color.self) { color in
ColorView(color: color)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
NavigationStack {
ContentView()
.environmentObject(Router())
}
}
}
ColorView.swift
import SwiftUI
struct ColorView: View {
@EnvironmentObject var router: Router
var color: Color
var body: some View {
color
.navigationTitle(color.description.capitalized)
.toolbar {
NavigationLink("info", value: true)
}
.navigationDestination(for: Bool.self) { bool in
TextView()
}
}
}
struct ColorView_Previews: PreviewProvider {
static var previews: some View {
NavigationStack {
ColorView(color: .green)
.environmentObject(Router())
}
}
}
TextView.swift
import SwiftUI
struct TextView: View {
@EnvironmentObject var router: Router
var body: some View {
VStack {
Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
.frame(alignment: .leading)
.padding()
}
Spacer()
.navigationTitle("info")
.toolbar {
Button ("home") {
router.reset()
}
}
}
}
struct TextView_Previews: PreviewProvider {
static var previews: some View {
NavigationStack {
TextView()
.environmentObject(Router())
}
}
}