Arrays 从复杂的自定义数组中删除

Arrays 从复杂的自定义数组中删除,arrays,swift,Arrays,Swift,我有一个自定义对象数组 var shopList = [String: [ShopItem]]() 自定义类 class ShopItem { var id = "" var name = "" var quantity = 0.0 var price = 0.0 var category = "" init(id: String, name: String, quantity: Double, price: Double, category: String) { self.id

我有一个自定义对象数组

var shopList = [String: [ShopItem]]()
自定义类

class ShopItem {
var id = ""
var name = ""
var quantity = 0.0
var price = 0.0
var category = ""


init(id: String, name: String, quantity: Double, price: Double, category: String) {
    self.id = id
    self.name = name
    self.quantity = quantity
    self.price = price
    self.category = category
}


var uom: String {

    return "шт."
}

var total: Double {
    return quantity * price
}
}

从数组中删除对象的正确方法是什么? 我试着在下面做

但正如您看到的,我得到了错误:(

由于值语义(对象被复制而不是引用),
对象是不可变的。即使您将
分配给变量,对象也不会在
购物清单
字典中删除

您需要直接删除字典中的对象(代码为Swift 3)


只需使用enumerated()方法并使用元素偏移量更改数组元素,请给出示例:)
extension ShopItem: Equatable {}
func ==(left: ShopItem, right: ShopItem) -> Bool {
return left.id == right.id
}
func removeItem(item: ShopItem) {
    for (key, value) in shopList {
        if let index = value.index(of: item) {
            shopList[key]!.remove(at: index)
        }
    }
}