Cannot assign to property: "Example" is a get-only property

I'm working on an exercise app and have been running into the error "Cannot assign to property: "weightArray" is a get-only property".

I want to have the textfield be able to enter a different numerical value for each set (0..<4)

I believe I need to use { get set } in weightArray in my model data but I can't seem to figure out exactly how to implement it.

View:

    @Binding var data: Exercise
    var body: some View {
        List {
            VStack(alignment: .leading) {
                ForEach(0..<4, id: \.self) { sets in
                    ZStack {
                        RoundedRectangle(cornerRadius: 8)
                            .fill(.opacity(0.1))
                        TextField("0", text: $data.weightArray[sets])
                            .multilineTextAlignment(.center)
                            .keyboardType(.decimalPad)
                    }
                    .frame(width: 65, height: 30)
                    Text("lbs")
                    Divider()
                }
            }
        }
    }
}

Model:

struct Exercise: Identifiable, Codable {
        let id: UUID
        var weight: [String]

        var weightArray: [String] {
            let array: Array<String> = Array(repeating: "0", count: numericalSets)
            return array
        }

Any help is appreciated.

You declare weightArray as a computed var. So it has no storage in which to store value, just a way to compute from other properties.

You don't show whole code, so hard to tell precisely.

  • Why is data a Binding ?
  • why do you have 2 var weightArray and weight ? What is the use of weight ?
  • Where is numericalSets defined ?

With some guessing, here is a code that works. But not sure it is doing what your code is supposed to do.

let numericalSets = 4   // Guessing

struct ContentView: View {
    @State var data = Exercise() // Why a Binding ? @Binding var data: Exercise
    
    var body: some View {
        List {
            VStack(alignment: .leading) {
                ForEach(0..<numericalSets, id: \.self) { sets in    // I guess 4 is for numericalSets
                    ZStack {
                        RoundedRectangle(cornerRadius: 8)
                            .fill(.opacity(0.1))
                        TextField("0", text: $data.weightArray[sets])
                            .multilineTextAlignment(.center)
                            .keyboardType(.decimalPad)
                    }
                    .frame(width: 65, height: 30)
                    Text("lbs")
                    Divider()
                }
            }
        }
    }
}

struct Exercise: Identifiable, Codable {
    var id: UUID = UUID()       // Better to initialise here ; var to be Codable
    // Seems unused var weight: [String]
    var weightArray: [String]
    
    //        var weightArray: [String] {
    //            let array: Array<String> = Array(repeating: "0", count: numericalSets)
    //            return array
    //        }

    init() {
        weightArray = Array(repeating: "0", count: numericalSets) // numericalSets, but where is it defined ?
    }
}

Cannot assign to property: "Example" is a get-only property
 
 
Q