Thanks for showing your code. But in your
physicsPage, the identifier
$showingSheet (or
showingSheet) is obviously not visible.
You may need to fix it
first. (There are some other points to fix.)
To make an example, I have modified your code as follows:
Code Block | import SwiftUI |
| struct ContentView: View { |
| |
| @State private var showingSheet = false |
| |
| var body: some View { |
| ZStack{ |
| VStack{ |
| Text("Reference Buddy :D") |
| .font(.title) |
| .fontWeight(.bold) |
| HStack{ |
| Button("Physics") { |
| self.showingSheet.toggle() |
| } |
| .sheet(isPresented: $showingSheet) { |
| PhysicsPage(showingSheet: $showingSheet) //<- |
| } |
| Button("Trigonometry", action: {}) |
| Button("Calculus", action: {}) |
| } |
| HStack{ |
| Button("Pre-calculus", action: {}) |
| Button("Geometry", action: {}) |
| Button("Statistics", action: {}) |
| } |
| HStack{ |
| Button("Chemistry", action: {}) |
| Button("Biology", action: {}) |
| Button("Astronomy", action: {}) |
| } |
| } |
| } |
| } |
| } |
| struct ContentView_Previews: PreviewProvider { |
| static var previews: some View { |
| ContentView() |
| } |
| } |
| struct PhysicsPage: View { |
| @Environment(\.presentationMode) var presentationMode |
| @Binding var showingSheet: Bool //<- |
| var body: some View { |
| VStack{ |
| Text("Did you know that...") |
| ForEach((1...10), id: \.self) { |
| Text("\($showingSheet)") |
| } |
| Button("Back") { |
| self.presentationMode.wrappedValue.dismiss() |
| } |
| } |
| } |
| } |
This code generates 5 errors with Xcode 12.0.1.
Code Block | Extra arguments at positions #2, #3 in call |
| Missing argument for parameter 'content' in call |
| Cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<Int>' |
| Generic parameter 'Content' could not be inferred |
| Cannot infer key path type from context; consider explicitly specifying a root type |
As you have shown in your original post.
The most important I think is your #1:
Code Block | 1. "Cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<Int>' |
In the SwiftUI struct
ForEach, there is no initializer taking
ClosedRange<Int> (generated with operator
...), but there is one taking
Range<Int> (generated with
..<).
ForEachinit(Range<Int>, content: (Int) -> Content)The initializer takes
Range<Int> and has no parameter named
id:.
With using it:
Code Block | struct PhysicsPage: View { |
| @Environment(\.presentationMode) var presentationMode |
| @Binding var showingSheet: Bool //<- |
| var body: some View { |
| VStack{ |
| Text("Did you know that...") |
| ForEach(0..<10) {_ in //<- |
| Text("\($showingSheet)") |
| } |
| Button("Back") { |
| self.presentationMode.wrappedValue.dismiss() |
| } |
| } |
| } |
| } |
This code still generates one error, but it is more understandable than the former five errors (at least, for me):
Code Block | Instance method 'appendInterpolation' requires that 'Binding<Bool>' conform to '_FormatSpecifiable' |
In the String Interpolation passed to the first argument of
Text, only some limited types are usable.
If you do not know about the types available, you should better use
String:
Code Block | struct PhysicsPage: View { |
| @Environment(\.presentationMode) var presentationMode |
| @Binding var showingSheet: Bool //<- |
| var body: some View { |
| VStack{ |
| Text("Did you know that...") |
| ForEach(0..<10) {_ in //<- |
| Text("\(String(describing: $showingSheet))") //<- |
| } |
| Button("Back") { |
| self.presentationMode.wrappedValue.dismiss() |
| } |
| } |
| } |
| } |
Your actual code may be a little different than you have shown, but I think you can fix all errors following the fixes I have shown here.