ios – SwiftUI: Binding cannot assign property?


This is my entire code:

import SwiftUI
import Observation


struct CarModel: Identifiable {
    var id = UUID()
    var name:String
}

@Observable class TestModel {
    
    var cars = [CarModel(name: "__-1-_1") ]
    
}




struct ContentView: View {
    
    @State var model = TestModel()
    
    var body: some View {
        
        VStack {
            ScrollView {
                LazyVStack {
                    ForEach($model.cars) { c in
                        if c.name.count > 0 { // ERROR -> Cannot assign to property: 'count' is a get-only property
                            CarView(car: c)
                        } else {
                            Text("test")
                        }
                    }
                }
            }
        }
        .padding()
    }
}

#Preview {
    ContentView()
}



struct CarView: View {
    
    @Binding var car:CarModel
    
    var body: some View {
        
        Text("The car \(car.name)")
    }
    
}

I am not sure how to get rid of that error. All I want to do is to do a count on the name but the Binding throws it off.

I can keep the binding and simply also use the c.name like I am trying to do here, whether it’s a String.count or any other string operation on name, nothing works.

Thank you

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img