I cannot seem to find a solution to this after much searching. I am trying to update my state variable d in the function call addDay. This function is called in the ForEach loop. The problem is, d is updated once to a value of 2.0, then it stays at 2.0 forever. I'm not sure what is wrong, any insight?
let weekdays: [String] = ["Mon", "Tues", "Wed", "Thurs", "Fri"]
@State var startDate: Date = Date().next(.monday, direction: .backward)
@State var d:Double = 1.0
/**
- parameter startDate: the beginning date that will be modified
- Returns: A new Text that has a date attached
*/
func addDay(startDate: Date) -> Text {
self.startDate = startDate.addingTimeInterval(86400 * self.d)
self.d += 1.0
return Text("\(startDate, formatter: Self.weekFormat)")
}
static let weekFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd"
return formatter
}()
var body: some View {
ScrollView(.horizontal) {
HStack{
ForEach(weekdays, id: \.self) { weekday in
VStack{
RoundedRectangle(cornerRadius: 9, style: .continuous)
.stroke(Color.blue)
.frame(width: 68.5, height: 68.5)
self.addDay(startDate: self.startDate)
Text(String(self.d))
Text(weekday)
}
}
}
}
}
}
