I am trying to watch an @EnvironmentObject in the Xcode debugger and it comes up as an Invalid Expression in the View pane. If I want to view it, I need to declare it as a local variable and then assign it the value of the passed in @EnvironmentObject.
While not impossible, it's a lot of work. Or maybe I am doing something incorrectly and this is a symptom of that? Would like to resolve this.
encl: Example code & Screenshot, where choice is the passed/unviewable EnviornmentObject and ch is the local variable I use to view it in the debugger
Project "TestingApp"
File: TestingApp
import SwiftUI
@main
struct TestingApp: App {
@StateObject private var choice = Choices()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(choice)
}
}
}
File: Choices
import Foundation
@MainActor
class Choices: ObservableObject {
@Published var aChoice = 1
@Published var bChoice = 2
}
File: ContentView
import SwiftUI
struct ContentView: View {
@EnvironmentObject private var choice: Choices
var body: some View {
VStack {
let ch = choice
Text("\(choice.aChoice)")
.font(.largeTitle)
.padding(.bottom)
Text("2")
.font(.largeTitle)
}
.padding()
}
}
#Preview {
ContentView()
}