I'm trying to make the final app Order resemble a bill
Instead of having multiple rows with the same item, i'm trying to group items:
3 x Full English
2 x Coke
1 x Stake
but the following code runs into issues:
func add(item: MenuItem) {
let index = (items.firstIndex(of: item))
if (index == nil) {
print("could not find first item")
items.append(item)
} else if (index) != nil {
print("found first item")
items[index!].qty += 1
}
}
to make the code above work you'll need to add a qty field to MenuItems.
The code above works for a new item and incrementing that item but then unexplainably adds a new item.
If you have a more alegant solutions please enlighten me?
struct MenuItem: Codable, Equatable, Identifiable {
var id: UUID
var name: String
var photoCredit: String
var price: Int
var restrictions: [String]
var description: String
var qty: Int = 1
Thank you very much, with your help I managed to get everything working.
let index = items.firstIndex(where: {$0.id == item.id})
if (index != nil) {
items[index!].qty += 1
} else {
items.append(item)
}