I'm trying to access UndoManager
via Environment
somewhere deep in the navigation hierarchy:
import SwiftUI
@main
struct TestUndoManagerEnvironmentApp: App {
var body: some Scene {
DocumentGroup(newDocument: TestUndoManagerEnvironmentDocument()) { file in
WrapperContent()
}
}
}
struct WrapperContent: View {
@Environment(\.undoManager) private var undoManager: UndoManager!
var body: some View {
NavigationLink(destination: ContentView()) {
Text("go")
}
Text("\(undoManager.canRedo ? "t" : "f")")
}
}
struct ContentView: View {
@Environment(\.undoManager) private var undoManager: UndoManager!
var body: some View {
Text("\(undoManager.canRedo ? "t" : "f")")
}
}
And whenever I run the app I get the Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
in ContentView.body
.
Am I doing something clearly wrong?