I am creating a little workout app using SwiftUI. I have a list of exercises stored in Core Data, and when the user selects one from the list I am adding it to an array in the state.
@State private var workoutExercises: [CDWorkoutExercise] = []
...
func onSelectExercise(exercise: CDExercise) {
let newWorkoutExercise = CDWorkoutExercise(context: self.moc)
newWorkoutExercise.exercise = exercise
newWorkoutExercise.reps = 8
newWorkoutExercise.sets = 3
workoutExercises.append(newWorkoutExercise)
}
I have a ForEach that loops over the exercise core data objects that have been added to the array and display the exercise they added as well as allow the user to use an input, preferably a Stepper or Textfield, to change the number of reps to perform for the exercise.
ForEach(workoutExercises, id: \.self) { workoutExercise in
VStack {
Text(workoutExercise.exercise?.wrappedName ?? "Unknown")
Stepper("Reps", value: $workoutExercise.reps, in: 1...100)
}
}
However, when I try to bind the object to the input Xcode displays the error Use of unresolved identifier: $workoutExercise (in this case on the line where the Stepper is defined) and I'm unsure how to resolve the issue or where I've gone wrong.
stateful object, you can't access it'sprojectedValue. That effectively means, theCDWorkoutExercisetype must be represented as an object which can be observed of its change over time.