Problème comes from, not from command.
@State public var contentView = ContentView()
I have tested this very simple code to show:
struct ContentView: View {
public func importTerms() {
print("\(showFileImporter)")
showFileImporter = true
print("\(showFileImporter)")
}
var body: some View {
Button("Test") {
importTerms()
}
}
}
struct ContentView2: View {
@State private var content = ContentView()
var body: some View {
Button("Test2") {
content.importTerms()
}
}
}
If I call ContentView(), and tap "Test", I get the correct result.
false
true
But If I call ContentView2(), and tap "Test2", I get the wrong result, var is not changed.
false
false
My guess is that with this declaration of state var ContentView, you create 2 instances, and there is a confusion in the showFileImporter state var at system level.
you update var for instance2 which is ignored by the system which uses instance1 (in print).
Someone more expert in SwiftUI may provide more accurate explanation.
You could use environment variables for showFileImporter and remove the contentView state var to solve the issue.
Note: in Swift, you don't need to end statements with semicolon.