Hi. So I have been following along the excellent tutorial until Chapter 10 when we are asked to do the challenge to fix the design of HIITFit. The project supplied in the challenge directory contains some new code that I especially do not understand and I was hoping for some clarification.
My question is on the code that was written in the .onAppear modifier, I will paste it below. I was adding my own comments to better understand what is going on
var body: some View {
GeometryReader { geometry in
VStack {
HStack {
ForEach(0…<7) { index in
bar(day: index, size: geometry.size)
}
}
Divider()
Spacer()
}
.onAppear {
days = Date().lastSevenDays //array of dates from past week
exercisesForWeek = ExerciseDay
let counts: [Int] = days.map { day in
let foundDate = exercisesForWeek.filter {
$0.date.yearMonthDay == day.yearMonthDay
}
return foundDate.first?.exercises.count ?? 0
//returns the number of exercises completed for the first day
}
assert(counts.count == 7)
// remap values to 0 to maxBarHeight
let maxValue = max(counts.max() ?? 0, 1)
countsForWeek = counts.map {
$0 * maxBarHeight / maxValue
}
}
I understand that foundDate is an array of ExerciseDays, an array of exercises that were completed in the past week, and that counts as an array of integers, is an array of the number of exercises that were completed in the first day, the first ExerciseDay element
return foundDate.first?.exercises.count ?? 0
My question is, what is the purpose of counts as a value, since it is the number of exercises done on the first day? And given that our development data does not have 7 exercises, what is the purpose of the assert line?
assert(counts.count == 7)
The assertion is going to fail every time, and I am positively flummoxed as to the logic behind this code. Any help would be appreciated, if I hope to reproduce what I have learnt in an app of my own I feel I have to understand the logic behind the supplied code.
Thanks in advance