I am trying to make a checkout screen for an ordering app. I made a struct called dish which has the price as a property. I want to display the total, but I don't know how to total up the prices. I've attempted to use cart.reduce(0, +), but it gives me the error "Ambiguous reference to member '+'".
Any help/advice would be greatly appreciated!
Struct and struct array:
//My struct
struct Dish {
var price: Double
}
//Struct array
var cart: [Dish] = []
Adding to cart:
//Add an item to cart
@IBAction func DishTwoOrdered(_ sender: Any) {
cart.append(Dish(price: 7.50))
checkoutQuantity.text = "In your cart: \(cart.count)"
}
Adding indices:
@IBAction func ringUp(_ sender: Any) {
//Error here
let sum = cart.reduce(0, +)
sumLabel.text = "Total: \(sum)"
}
let sum = cart.reduce(0) { $0 + $1.price }stackoverflow.com/a/49046981/2303865