I'm trying make it so that when the plus button is pushed in ListView the price will increment by one and be reflected on the screen. I'm not getting any errors, and I'm getting the expected result from the print statement, but the view will not update. I've tried just incrementing the price directly in the button as opposed to creating a variable for the same thing. but I get an error saying the left side is immutable because it's a let constant. Any help would be appreciated, I'm getting really frustrated. Thank you!
struct Product: Identifiable {
let id = UUID()
var name: String
var price: Double
var aisle: Int
var location: Int
}
class Inventory {
var inventory: [Product] = [
Product(name: "Coke", price: 2.99, aisle: 1, location: 10),
Product(name: "Pepsi", price: 3.99, aisle: 1, location: 6),
Product(name: "Dr. Pepper", price: 1.99, aisle: 2, location: 8),
Product(name: "Pibb", price: 1.50, aisle: 2, location: 1)
]
}
struct ListView: View {
@State var base = Inventory().inventory
@State var userList: [Product] = []
var body: some View {
List {
ForEach(base) { product in
var thing = product.price
HStack {
Text(product.name)
Spacer()
Button {
thing += 1
print(thing)
} label: {
Image(systemName: "plus")
}
Text("\(thing, specifier: "%.2f")")
}
}
}
}
}
struct ListView_Previews: PreviewProvider {
static var previews: some View {
ListView()
}
}
2
0
2k