why it is throw an error as a Value of type 'ContentView' has no member 'restaurants' in SwiftUI?

I have example of swiftUI project, and it is throw an error as a "Value of type 'ContentView' has no member 'restaurants'" for

   self.restaurants.remove(atOffsets: indexSet) }

line of code, I do not know what I missed, any idea?

import SwiftUI

struct ContentView: View {
   
  
  @State private var selectedRestaurant: Restaurant?
   
  var body: some View {
    List {
      ForEach(restaurants) { restaurant in
        BasicImageRow(restaurant: restaurant)
          .contextMenu {
             
            Button(action: {
              // mark the selected restaurant as check-in
              self.checkIn(item: restaurant)
            }) {
              HStack {
                Text("Check-in")
                Image(systemName: "checkmark.seal.fill")
              }
            }
             
            Button(action: {
              // delete the selected restaurant
              self.delete(item: restaurant)
            }) {
              HStack {
                Text("Delete")
                Image(systemName: "trash")
              }
            }
             
            Button(action: {
              // mark the selected restaurant as favorite
              self.setFavorite(item: restaurant)
            }) {
              HStack {
                Text("Favorite")
                Image(systemName: "star")
              }
            }
          }
          .onTapGesture {
            self.selectedRestaurant = restaurant
          }
          .actionSheet(item: self.$selectedRestaurant) { restaurant in
             
            ActionSheet(title: Text("What do you want to do"), message: nil, buttons: [
               
              .default(Text("Mark as Favorite"), action: {
                self.setFavorite(item: restaurant)
              }),
               
              .destructive(Text("Delete"), action: {
                self.delete(item: restaurant)
              }),
               
              .cancel()
            ])
          }
      }
      .onDelete { (indexSet) in
        self.restaurants.remove(atOffsets: indexSet)
      }
    }
  }
   
  private func delete(item restaurant: Restaurant) {
    if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {
      self.restaurants.remove(at: index)
    }
  }
   
  private func setFavorite(item restaurant: Restaurant) {
    if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {
      self.restaurants[index].isFavorite.toggle()
    }
  }
   
  private func checkIn(item restaurant: Restaurant) {
    if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {
      self.restaurants[index].isCheckIn.toggle()
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

struct BasicImageRow: View {
  var restaurant: Restaurant
   
  var body: some View {
    HStack {
      Image(restaurant.image)
        .resizable()
        .frame(width: 40, height: 40)
        .cornerRadius(5)
      Text(restaurant.name)
       
      if restaurant.isCheckIn {
        Image(systemName: "checkmark.seal.fill")
          .foregroundColor(.red)
      }
       
      if restaurant.isFavorite {
        Spacer()
         
        Image(systemName: "star.fill")
          .foregroundColor(.yellow)
      }
    }
  }
}

model:

struct Restaurant: Identifiable {
  var id = UUID()
  var name: String
  var image: String
  var isFavorite: Bool = false
  var isCheckIn: Bool = false
}

var restaurants = [ Restaurant(name: "Cafe Deadend", image: "cafedeadend"),
       Restaurant(name: "Homei", image: "homei"),
       Restaurant(name: "Teakha", image: "teakha"),
       Restaurant(name: "Cafe Loisl", image: "cafeloisl"),
]
Answered by Claude31 in 705847022

In

      .onDelete { (indexSet) in
        self.restaurants.remove(atOffsets: indexSet)
      }

you tell compiler, by prefixing by self., that restaurants is a property of ContentView. Which it is not.

Just replacing by

      .onDelete { (indexSet) in
        restaurants.remove(atOffsets: indexSet)
      }

should make it work, as restaurants now refers to the var restaurants.

If any question, look at Swift documentation about scope of variables.

Accepted Answer

In

      .onDelete { (indexSet) in
        self.restaurants.remove(atOffsets: indexSet)
      }

you tell compiler, by prefixing by self., that restaurants is a property of ContentView. Which it is not.

Just replacing by

      .onDelete { (indexSet) in
        restaurants.remove(atOffsets: indexSet)
      }

should make it work, as restaurants now refers to the var restaurants.

If any question, look at Swift documentation about scope of variables.

why it is throw an error as a Value of type 'ContentView' has no member 'restaurants' in SwiftUI?
 
 
Q