I have this Swift code here:
struct chapter: View {
var body: some View {
Form {
ForEach(1..<18) { _ in
NavigationLink(destination: chapterrun()) {
Text("Chapter \($0)")
}
}
}
.navigationTitle("App")
}
}
But it gives the error:
Contextual closure type '() -> Text' expects 0 arguments, but 1 was used in closure body
How do I fix this?
>Solution :
You have two closures, so $0 from first one (even if it would be correct, but with _ in you just ignore it) is not available in second one.
The fix is to use argument explicitly, like
ForEach(1..<18) { index in // << here !!
NavigationLink(destination: chapterrun()) {
Text("Chapter \(index)") // << here !!
}
}