-2
我想检查传递给CartViewController
的数组是否包含重复项,如果是的话我想将数量变量加1。我只是不知道从哪里开始。我可以使用filter
或其他东西来检查cart
产品对象是否已包含在内。检查数组是否重复
struct Cart{
var product: Product!
var quantity: Int = 0
init(prod: Product, quantity: Int = 1){
product = prod
self.quantity = quantity
}
}
Pseudeo代码
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())
}
}
}
此答案可能会帮助您:http://stackoverflow.com/a/29730259/4557505 – HardikDG