Delete items of an class's array from a view... (?)

Hi I am really new coding in swift so sorry if I'm asking something stupid. But I can't see the error or the solution an I don't know what to do :( I have this class and structure :

   @Published var items : [Items] = []
   @Published var showButtons = false

       
       func addItem(id: Int, category: String, image: String) {
           items.append(Piercing(id: id, category: [category], image: image))
       }

       func deleteItem(item: Item) {
           if let index = items.firstIndex(of: item) {
               item.remove(at: index)
           }
       }
       
       init() { }
       
}

struct Item: Identifiable, Decodable, Equatable {
    var id: Int
    var category: [String]
    var image: String
}

then I create a view for this:

struct ItemView: View {
    var item: Item
    @State private var position = CGPoint(x: 100, y: 100)
    @ObservedObject var items: ItemModel = .init()
    @State  var showButtons = false
    var body: some View {
        
        if items.showButtons {
                    Button (role: .destructive) {
                        withAnimation {items.deleteItem(item: item)}
                        
                    } label:{
                            Image(systemName: "trash")                         
                                .font(.body)
                                .bold()          
                }
            }
            
            VStack {
                    Image(item.image)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .position(position)
                        .onTapGesture(count: 1, perform: {
                            withAnimation {  items.showButtons.toggle()}
                        })
            }
        }
    }

in my ContentView I have this @StateObject var items: ItemModel = .init()

Right, from another View I add Items to ContentView perfectly, and from the ContentView I can deleteItems too... but I want to add a button in this view can't delete item from the button I have in this view calling deleteItem function from here... Any idea of what I'm doing in a wrong way...

really thanks!

Right, from another View I add Items to ContentView perfectly, and from the ContentView I can deleteItems too... but I want to add a button in this view can't delete item from the button I have in this view calling deleteItem function from here... Any idea of what I'm doing in a wrong way...

It is really not clear what views you are speaking of.

Please show code where we can see the view name and give the name explicitly (not as 'another' view or 'this' view).

If you have @StateObject var items: ItemModel = .init() in your ContentView, you should pass this model to the other views, for example @ObservedObject var items: ItemModel in your ItemView.

Also you should have in your Observable class, items.append(Item(id: id, category: [category], image: image)) not Piercing(...).

Currently you create a new and different ItemModel every time you show ItemView

Also you should have "@Published var items : [Item] = []" note, NO s in "[Item]". Conversely, in your delete, use "items.remove(at: index)"

Delete items of an class's array from a view... (?)
 
 
Q