Cannot convert value of type '[Lesson]' to expected argument type 'Binding<C>' and Generic parameter 'C' could not be inferred

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
ForEach(lessons) { lesson in ...} 

is not a "normal" for loop. It is a View that needs to have other Views in it, not procedural code like you have.

Put this type of code (using a "normal" for loop`), for example in

.onAppear{ 
       lessons.forEach{ lesson in ...} 
}

Note that NavigationView is deprecated, use NavigationStack instead.

You have other errors in your code.

  • liveHourString is declared twice
  • currentLessonStart not defined
  • startTime is a String in testData, but should be a Date
  • You should change currentLesson when you tap a lesson in List, not in the loop
            NavigationView {
                List(lessons) { lesson in
                    if lesson.dayOfWeek == todaysDate.formatted(Date.FormatStyle().weekday(.wide)) {
                        VStack(alignment: .leading){
                            Text(lesson.name)
                                .onTapGesture {
                                    currentLesson = lesson.name
                                    currentLessonStart = lesson.startTime // Once you have defined a State variable for currentLessonStart as a Date var
                                }

You could also use computed var to initialise lessons:

struct ContentView: View {
//    var lessons: [Lesson] = []
    var liveHourString : String {
        todaysDate.formatted(date: .omitted, time: .shortened)
    }
    var liveWeekdayString : String {
        todaysDate.formatted(Date.FormatStyle().weekday(.wide))
    }
    var lessons: [Lesson] {
        var listOfLessons : [Lesson] = []
        for lesson in testData {
                if lesson.startTime <= liveHourString && lesson.endTime >= liveHourString && lesson.dayOfWeek == liveWeekdayString {
                    // currentLesson = lesson.name // YOU Cannot change the State var here
                   //   currentLessonStart = lesson.startTime
                }
            listOfLessons.append(lesson)
            }
        return listOfLessons
    }
    

Another thing.

If you don't want to select from List but just display the current lesson, build other computed varsity, such as these:

    /* @State private */var currentLesson: String { // = "Przerwa"
        for lesson in testData {
                if lesson.startTime <= liveHourString && lesson.endTime >= liveHourString && lesson.dayOfWeek == liveWeekdayString {
                    var components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: todaysDate)
                    let hhmm = lesson.startTime.split(separator: ":")
                    components.hour = Int(hhmm[0])
                    components.minute = Int(hhmm[1])
                    return lesson.name
                }
            }
        return ""
    }
    /* @State private */var currentLessonStart : Date { // = Date()
        for lesson in testData {
                if lesson.startTime <= liveHourString && lesson.endTime >= liveHourString && lesson.dayOfWeek == liveWeekdayString {
                    var components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: todaysDate)
                    let hhmm = lesson.startTime.split(separator: ":")
                    components.hour = Int(hhmm[0])
                    components.minute = Int(hhmm[1])
                    return Calendar.current.date(from: components)!
                }
            }
        return Date()  // If not found
    }
Cannot convert value of type '[Lesson]' to expected argument type 'Binding&lt;C&gt;' and Generic parameter 'C' could not be inferred
 
 
Q