For context I have the following HabitList Class and Habit Struct
struct Habit: Identifiable, Codable{
let id = UUID()
let name: String
var hours: Double
let description: String
}
class HabitList: ObservableObject{
@Published var habitList = [Habit](){
}
}
and in my ContentView I want to be able to change the hours
attribute via Stepper in a ForEach Loop.
ForEach(habitList.habitList){habit in
Stepper("Hours: ", value: $habit.hours)
}
However I am getting an error Cannot find '$habit' in scope
, though I am not sure exactly why this particular message is showing up, I suspect that my issue is related to the fact that habit.hours is not a State Variable of the ContentView and thus I cannot pass it in like a binding..?
I cannot simply change my var hours: Double
in the Habit struct to @State var hours: Double
because then I get an error that the State
attribute is unknown. I suspect that this is because Habit
is not a view, but please correct me if that is incorrect.
I have managed to work with bindings before when I am binding a State property of the view, but I'm a little confused as to how I should bind an attribute of another type of struct.
I tried to be as clear as possible but let me know if anything needs clarification.
Thanks!