Reputation: 41
Model Class
struct Products: Identifiable {
var id = UUID()
var title:String
var price:String
var image:String
}
I am trying to change the new value of my product price by button click it showing (Cannot assign to property: '_products' is a 'let' constant)
(Cannot assign to property: '_products' is a 'let' constant)
I am trying to change the new value of my product price by button click it showing (Cannot assign to property: '_products' is a 'let' constant)
Upvotes: 2
Views: 3255
Reputation: 41
class Products: ObservableObject, Identifiable, Codable {
var id = UUID()
var title:String
var price:String
var image:String
init(title: String, price: String, image: String){
self.title = title
self.price = price
self.image = image
}
}
Upvotes: 0
Reputation: 30371
You can use the new syntax where you can have a Binding
in a ForEach
, giving each element as a Binding
. Link here to useful article about it.
This means code such as this (where item
is a constant):
ForEach(items) { item in
/* ... */
/* item */
}
Can now be transformed to this (where $item
is a Binding
, and item
can now be set):
ForEach($items) { $item in
/* ... */
/* item and $item */
}
Basically now, change your ForEach
to this:
ForEach ($Topproducts) { $product in
And for every usage which was previously _products
, replace it with product
. You can now mutate the value.
Upvotes: 3