How we can delete all list items in SwiftUI?

I have a simple app in SwiftUI, and I try to delete all list items with context menu , when I click context menu button, I want to remove all items, is it possible?

struct MyView: View {

    @State private var selectedUsers: MyModel?
 
    var body: some View {
        
            ScrollView(.vertical, showsIndicators: false, content: {
                
                VStack(content: {
                    
                    ForEach(datas){ data in
  
                        MyRowView(data: data)
                         
                            .contextMenu {

                                Button(action: {
                                    self.delete(item: data)
                                }) {
                                    Text("delete") 
                                }
                               
                            }
                            .onTapGesture {
                                selectedUsers = data
                            
                            }
                        
                    } .onDelete { (indexSet) in
                        selectedUsers.remove(atOffsets: indexSet)
                    }})
           })}

    private func delete(item data: MyModel) {
               if let index = datas.firstIndex(where: { $0.id == data.id }) {
                   datas.remove(at: index)
               }
           }}

model:

struct MyModel: Identifiable, Hashable, Codable {

    var id = UUID().uuidString
    var name: String
    }

  var datas = [
    
    MyModel(name: "david"),
    MyModel(name: "marry"),
  ]

So you want to delete all datas and not only one data ?

It may not be very intuitive, UI speaking, to propose this from an item. Why not a general button to remove all, just by making datas = []

But if you want the other way, did you try:

Button(action: {
    datas = []
}) {
    Text("delete all data") 
}

Hi I'm Davide. In my case I have a problem. Xcode report an error:

Cannot find 'MyRowView' in scope

How can I solve?

How we can delete all list items in SwiftUI?
 
 
Q