Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Contextual closure type '() -> Text' expects 0 arguments, but 1 was used in closure body

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 !!
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading