I want to check if the array being passed to the CartViewController
contains duplicates if so I want to increment the quantity variable by one. I'm just not sure where to start. Could I use a filter
or something to check if the cart
product object is already contained.
struct Cart{
var product: Product!
var quantity: Int = 0
init(prod: Product, quantity: Int = 1){
product = prod
self.quantity = quantity
}
}
Pseudeo code
var cartItems = [Product]()
var finalCart = [Cart]()
override func viewDidLoad() {
for product in cartItems{
if finalCart.contains(product){
finalCart increment index of current product by 1
}else{
finalCart.append(Cart.init())
}
}
}