I am quite new in Swift and SwiftUI and I am getting a error that I cannot understand.
Here is my class that I load with JSON from a web service
class ResaListViewModel: ObservableObject {
@Published var resasDeparts = [ResaViewModel]()
init() {
WSResa().getDeparts { resas in
if let resas = resas as? [Resa] {
self.resasDeparts = resas.map(ResaViewModel.init)
}
}
}
}
class ResaViewModel: Identifiable, ObservableObject {
@Published var resa: Resa
init(resa: Resa) {
self.resa = resa
}
var id: Int {
return Int(self.resa.id)!
}
var nbveh: Int
{
return Int(self.resa.nbveh) ?? 0
}
}
In a view I would like to Bind the nbveh property and to increment and decrement it with buttons.
the error is : "Cannot assign to property: 'nbveh' is a get-only property"
struct ResaDetail: View {
@ObservedObject var resa:ResaViewModel
var body: some View {
vehiculeView(nbveh: $resa.nbveh)
}
struct ResaView: View {
let resaListVM = ResaListViewModel()
var body: some View {
List(self.resaListVM.resasDeparts, id: \.id) { resa in
NavigationLink(destination: ResaDetail(resa: resa)) {
ResaList(resa: resa)
}
}
} .navigationBarTitle(Text("Réservations"))
}
How can I make this property editable and bindable ?
Thanks for any kind of help
-- Godefroy