0

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.

1
  • 2
    You need to make your CartModel conform to Equatable Commented Mar 28, 2018 at 15:33

1 Answer 1

1

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)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.