-1

I recently asked a question however it was removed as they thought the answer existed already and so I checked out this question. I have tried implementing the solution found here into my code and now run into a slightly different error.

Basically I am trying to create a simple application with a list of items that can be clicked on to navigate to a detail view with an edit feature. The data is read from a JSON file which is why the data is in a binding so it can be updated and this is not a problem anywhere else in the project.


import SwiftUI

struct EateryList: View {
    @Binding var eateries: [Eatery]
    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(eateries.indicies, id: \.self) { i in
                            NavigationLink(destination: EateryDetail(eatery: $eateries[i])) { //errors appear here
                               EateryRow(eatery: $eateries[i])
                           }
                        }
                }
                .navigationTitle("Favourite Eateries")
                .navigationBarItems(leading: EditButton(), trailing: Button( action: add)
                {
                    Image(systemName: "plus")
                    
                }
                    )
                     .listStyle(InsetGroupedListStyle())
            }
        }
        
    }
    func add() {
        eateries.append(Eatery(name: "Eatery", location: "Insert location here", notes: "Insert notes here", reviews: ["Insert reviews here"], url: "https://i.imgur.com/y3MMnba.png"))
    }
}

I get 3 errors with this code currently which I do not fully understand...

Generic struct 'ForEach' requires that 'Binding' conform to 'RandomAccessCollection'

Referencing subscript 'subscript(dynamicMember:)' requires wrapper 'Binding<[Eatery]>'

Value of type '[Eatery]' has no dynamic member 'indicies' using key path from root type '[Eatery]'

All help is greatly appreciated right now and if you need further clarification of other parts of the project please let me know :)

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
kmurp62rulz
  • 169
  • 2
  • 9

1 Answers1

1

It's a spelling mistake. It's indices not indicies

ForEach(eateries.indices, id: \.self) { i in //<-- Here
    NavigationLink(destination: EateryDetail(eatery: $eateries[i])) { 
        EateryRow(eatery: $eateries[i])
    }
}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52