ForEach loop keeps giving me an error

I get no error if my code is this
Code Block
ForEach((1...10), id: \.self) {
Text("\($0)")
}

But, if I change the \($0) to something else, like "bruh" or "\($variableName), it won't work. It gives me 5 errors
  1. "Cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<Int>'

  2. Cannot infer key path type from context; consider explicitly specifying a root type Insert '<#Root#>

  3. Extra arguments at positions #2, #3 in call

  4. Generic parameter 'Content' could not be inferred Explicitly specify the generic arguments to fix this issue

  5. Missing argument for parameter 'context' in call Insert ', content: <#(Int) -> _#>'

Can you show full code which gives you such errors.
@OOPer

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()
}
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
var body: some View {
VStack{
Text("Did you know that...")
ForEach((1...10), id: \.self) {
Text("\($showingSheet)")
}
Button("Back") {
self.presentationMode.wrappedValue.dismiss()
}
}
}
}

You need to replace by:

Code Block
            ForEach(1...10, id: \.self) {_ in 


I remember seeing on the forum a crash problem with ForEach, but impossible to search for it on this forum !

But Google found it !!! https://developer.apple.com/forums/thread/660718
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 ..<).
ForEach
init(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.
@OOPer

But
Code Block
ForEach((1...10), id: \.self) {

works
@Claude31 

But ... works

A fragment of code does not make sense. It may work with other parts fixed properly.

Just that it is hard to find where to fix.

In the SwiftUI struct ForEach, there is no initializer taking ClosedRange<Int> (generated with operator ...), but there is one taking Range<Int> (generated with ..<).
ForEach
init(Range<Int>, content: (Int) -> Content)
The initializer takes Range<Int> and has no parameter named id:.


The full fragment is:
Code Block
ForEach(1...10, id: \.self) {
Text("\($0)")
}

It works with a closed Range.

But this
Code Block
ForEach(1...10) {
Text("\($0)")
}

causes a compiler error.

Did I miss something ?
@OOPer, thank you so much. I found a fix.

Code Block
ForEach((1...10), id: \.self) {
Text("\($showingSheet)")
}

This doesn't work because showSheet is not in the same struct and also private.

After reading your answer, I changed my code to this:

Code Block
ForEach(0..<10) {_ in
Text("Hello")
}

The _ in part is required. If I remove that, I'l get an Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored error.

If I changed (0..<10) to (0...10), I get this error:
Cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<Int>'

Thank you so much :D
@TurtleLover

 thank you so much. I found a fix. 

You are welcome.
Seems you have found how to fix your code for some types of error messages.

But, please remember, the Swift compiler is not good at generating proper diagnostics especially when compiling SwiftUI views.
If you are unlucky, any of the error messages might not be pointing the right place to fix.

When you find some cases that you cannot fix by yourself, please do not hesitate to share your experience.
New questions will also be welcome.

ForEach loop keeps giving me an error
 
 
Q