0

I am trying to bind fileurls array to NavigationLink list I have an error

Cannot convert value of type 'Binding<[URL]>' to expected argument type 'Range'

struct ContentView: View {

    @State private var fileUrls:[URL] = []
    
    func listDir(dir: String) -> [URL] {
       // list file from directory 
    }
   
    
    var body: some View {
        NavigationView {
            ZStack {
                VStack {
                    List{
                        ForEach($fileUrls) { object in
                            NavigationLink(destination: PDFKitView(url: self.fileUrl1), isActive: $viewLocalPDF) {
                                Button("Swift Style"){
                                    self.viewLocalPDF = true
                                }.padding(.bottom, 20)
                                
                            }
                        }
                    }
                }
                .navigationBarTitle("Bookshelv ", displayMode: .inline).onAppear {
                    fileUrls = listDir(dir: "pdfs")
                }
                
            }
        }
}
NinjaDeveloper
  • 1,620
  • 3
  • 19
  • 51

1 Answers1

1

By using the $ you're referring to the Binding of fileUrls. You don't need the Binding for the ForEach, just the value.

Because URL doesn't conform to Identifiable, you'll also need to tell it how to identify each item. For this, you can just use \.self:

ForEach(fileUrls, id: \.self) { object in
jnpdx
  • 45,847
  • 6
  • 64
  • 94