Reputation: 3
I am using an array that is filled with structs:
struct CartModel: Codable {
var product_id: Int
var line_subtotal: Float
var line_total: Float
var line_tax: Float
var line_subtotal_tax: Float
var key: String
var quantity: Int
}
static var fullCart = [CartModel]()
Now I am trying to delete one of those from the array.
I tried to use fullCart.index(of:)
, but it is giving me the following error:
Argument labels '(of:, _:)' do not match any available overloads
I was hoping to use fullCart.remove(at:)
, but because of not being able to find the right index I am not sure how to remove the right item.
Upvotes: 0
Views: 204
Reputation: 6018
You also can find index by index(where:)
and then delete item from array:
if let index = fullCart.index(where: { $0.product_id == otherCartModel.product_id }) {
fullCart.remove(at: index)
}
Or conform to Equatable
protocol and use index(of:)
:
struct CartModel: Codable, Equatable {
...
static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.product_id == rhs.product_id
}
}
if let index = fullCart.index(of: otherCartModel) {
fullCart.remove(at: index)
}
Upvotes: 1