Accessing the isEditing property of environment value editMode leads to a compiler error.
In the code below I'm getting 'Generic parameter 'S' could not be inferred' applied to the Button
If I remove && !mode?.isEditing it compiles OK. I've tried juggling things around, but it always finds some problem. It looks like a bug to me either in @Environment or ViewBuilder. Any suggestions?
Here's the full struct:
struct ProjectView: View {
@Environment(\.editMode) var mode
@State private var canAddProject = false
@State private var showAlert = false
var projects: FetchedResults
var alert: Alert {
Alert(title: Text("Write Now"), message: Text("Please enter a project name"), dismissButton: .default(Text("Dismiss")))
}
var body: some View {
NavigationView {
List {
if !canAddProject && !mode?.isEditing {
Button("Add New Project") {}
.onTapGesture {
self.canAddProject.toggle()
}
}
if canAddProject {
AddProject(canAddProject: $canAddProject, showAlert: $showAlert)
}
ProjectList(projects: projects)
}
.listStyle(GroupedListStyle())
.navigationBarTitle(Text("Write Now"))
.navigationBarItems(trailing: EditButton())
}
.navigationViewStyle(StackNavigationViewStyle())
.alert(isPresented: $showAlert, content: { self.alert })
}
}
Thanks for this Jim. I tried this with the following:
struct ContentView: View {
@Environment(\.editMode) var mode
var body: some View {
NavigationView {
List {
if self.mode?.wrappedValue.isEditing ?? true {
Text("Editing")
}
else {
Text("Not Editing")
}
}
.navigationBarTitle(Text("Test Edit"))
.navigationBarItems(trailing: EditButton())
}
}
}
Unfortunately this didn't work - it just displayed "Not Editing" all the time. Suspecting this had to do with the edit button and mode being declared at the same level I refactored it to the following, which does work.
struct ContentView: View {
var body: some View {
NavigationView {
List {
DisplayEditor()
}
.navigationBarTitle(Text("Test Edit"))
.navigationBarItems(trailing: EditButton())
}
}
}
struct DisplayEditor: View {
@Environment(\.editMode) var mode
var body: some View {
if self.mode?.wrappedValue.isEditing ?? true {
return Text("Editing")
}
else {
return Text("Not Editing")
}
}
}