I have a problem with ForEach loop, and when i debug the program there appear a problems:
Cannot convert value of type '[Lesson]' to expected argument type 'Binding<C>'
Generic parameter 'C' could not be inferred
Please help me
import SwiftUI
import Foundation
struct ContentView: View {
var lessons: [Lesson] = []
let todaysDate = Date()
let lessonTime = Date()...Date().addingTimeInterval(45*60)
let startProgress = Date().addingTimeInterval(-60)
let endProgress = Date().addingTimeInterval(60)
@State private var currentLesson: String = "Przerwa"
var body: some View {
let liveHourString = todaysDate.formatted(date: .omitted, time: .shortened)
let liveHourString = "9:10"
let liveWeekdayString = todaysDate.formatted(Date.FormatStyle().weekday(.wide))
ForEach(lessons) { lesson in
if lesson.startTime <= liveHourString && lesson.endTime >= liveHourString && lesson.dayOfWeek == liveWeekdayString {
currentLesson = lesson.name
currentLessonStart = lesson.startTime
}
}
VStack {
ProgressView(timerInterval: startProgress...endProgress, countsDown: false) {
Text(currentLesson)
} currentValueLabel: {
Text(currentLessonStart...endProgress)
}
.padding([.top, .leading, .trailing], 15.0)
NavigationView{
List(lessons) { lesson in
if lesson.dayOfWeek == todaysDate.formatted(Date.FormatStyle().weekday(.wide)) {
VStack(alignment: .leading){
Text(lesson.name)
Text("\(lesson.startTime) - \(lesson.endTime)")
.foregroundStyle(.secondary)
Text(lesson.room)
.foregroundStyle(.secondary)
}
}
}
.navigationBarTitle(Text("Today's Lessons"))
}
}
}
}
#Preview {
ContentView(lessons: testData)
}
And i have also this file:
struct Lesson: Identifiable {
var id = UUID()
var name: String
var room: String
var startTime: String
var endTime: String
var dayOfWeek: String
}
#if DEBUG
let testData = [
//MONDAY
Lesson(name: "Programowanie Aplikacji Webowych", room: "J3", startTime: "8:00", endTime: "8:45", dayOfWeek: "Monday"),
Lesson(name: "Programowanie Aplikacji Webowych", room: "J3", startTime: "8:50", endTime: "9:35", dayOfWeek: "Monday"),
Lesson(name: "Programowanie Aplikacji Webowych", room: "J3", startTime: "9:40", endTime: "10:25", dayOfWeek: "Monday"),
Lesson(name: "Język Angielski", room: "Y2", startTime: "10:45", endTime: "11:30", dayOfWeek: "Monday"),
Lesson(name: "Język Niemiecki", room: "Y4", startTime: "11:40", endTime: "12:25", dayOfWeek: "Monday"),
Lesson(name: "Język Angielski", room: "Y2", startTime: "12:35", endTime: "13:20", dayOfWeek: "Monday"),
Lesson(name: "Matematyka", room: "B10", startTime: "13:25", endTime: "14:10", dayOfWeek: "Monday"),
//TUESDAY
Lesson(name: "WF", room: "Sala Gimnastyczna", startTime: "8:50", endTime: "9:35", dayOfWeek: "Tuesday"),
Lesson(name: "WF", room: "Sala Gimnastyczna", startTime: "9:40", endTime: "10:25", dayOfWeek: "Tuesday"),
Lesson(name: "Programowanie Aplikacji Desktopowych", room: "X7", startTime: "10:45", endTime: "11:30", dayOfWeek: "Tuesday"),
Lesson(name: "Programowanie Aplikacji Desktopowych", room: "X7", startTime: "11:40", endTime: "12:25", dayOfWeek: "Tuesday"),
Lesson(name: "Testowanie i Dokumentowanie Aplikacji", room: "J5", startTime: "12:35", endTime: "13:20", dayOfWeek: "Tuesday"),
Lesson(name: "Testowanie i Dokumentowanie Aplikacji", room: "J5", startTime: "13:25", endTime: "14:10", dayOfWeek: "Tuesday"),
Lesson(name: "Projektowanie Oprogramowania", room: "J5", startTime: "14:15", endTime: "15:00", dayOfWeek: "Tuesday"),
Lesson(name: "Projektowanie Oprogramowania", room: "J5", startTime: "15:05", endTime: "15:50", dayOfWeek: "Tuesday"),
//WEDNESDAY
Lesson(name: "Programowanie Aplikacji Mobilnych", room: "X1", startTime: "8:00", endTime: "8:45", dayOfWeek: "Wednesday"),
Lesson(name: "Programowanie Aplikacji Mobilnych", room: "X1", startTime: "8:50", endTime: "9:35", dayOfWeek: "Wednesday"),
Lesson(name: "Matematyka", room: "B10", startTime: "9:40", endTime: "10:25", dayOfWeek: "Wednesday"),
Lesson(name: "Matematyka", room: "B10", startTime: "10:45", endTime: "11:30", dayOfWeek: "Wednesday"),
Lesson(name: "Język Polski", room: "A7", startTime: "11:40", endTime: "12:25", dayOfWeek: "Wednesday"),
Lesson(name: "Język Polski", room: "A7", startTime: "12:35", endTime: "13:20", dayOfWeek: "Wednesday"),
Lesson(name: "Historia", room: "L5", startTime: "13:25", endTime: "14:10", dayOfWeek: "Wednesday"),
//THURSDAY
Lesson(name: "Chemia", room: "C7", startTime: "8:00", endTime: "8:45", dayOfWeek: "Thursday"),
Lesson(name: "WoS", room: "C8", startTime: "8:50", endTime: "9:35", dayOfWeek: "Thursday"),
Lesson(name: "Matematyka", room: "B10", startTime: "9:40", endTime: "10:25", dayOfWeek: "Thursday"),
Lesson(name: "Programowanie Obiektowe", room: "X1", startTime: "10:45", endTime: "11:30", dayOfWeek: "Thursday"),
Lesson(name: "Programowanie Obiektowe", room: "X1", startTime: "11:40", endTime: "12:25", dayOfWeek: "Thursday"),
Lesson(name: "Język Polski", room: "A7", startTime: "12:35", endTime: "13:20", dayOfWeek: "Thursday"),
//FRIDAY
Lesson(name: "Język Angielski", room: "Y2", startTime: "8:00", endTime: "8:45", dayOfWeek: "Friday"),
Lesson(name: "Chemia", room: "C7", startTime: "8:50", endTime: "9:35", dayOfWeek: "Friday"),
Lesson(name: "Geografia", room: "C10", startTime: "9:40", endTime: "10:25", dayOfWeek: "Friday"),
Lesson(name: "Matematyka", room: "B10", startTime: "10:45", endTime: "11:30", dayOfWeek: "Friday"),
Lesson(name: "Godzina Wychowawcza", room: "C15", startTime: "11:40", endTime: "12:25", dayOfWeek: "Friday"),
Lesson(name: "WF", room: "Sala Gimnastyczna", startTime: "12:35", endTime: "13:20", dayOfWeek: "Friday"),
Lesson(name: "Biologia", room: "C7", startTime: "13:25", endTime: "14:10", dayOfWeek: "Friday"),
]
#endif