Home > Back-end >  ForEach Error: Generic parameter 'C' could not be inferred
ForEach Error: Generic parameter 'C' could not be inferred

Time:08-04

I have two custom types: Weekday and Shift as shown below,

struct Weekday: Identifiable {

    var id: String = UUID().uuidString
    var name: String
    var index: Int
    var shifts: [Shift]
}

struct Shift: Identifiable, Encodable {
    var id: String = UUID().uuidString
    var startTime: Date
    var endTime: Date
}

I also have a variable in a class AvailabilityManager:

@Published var selectedWeekdays = [Weekday]

In my main view, I am trying to display a subview for each selected weekday. Additionally, within that subview, I want to display another subview for each shift.

ForEach(availabilityManager.selectedWeekdays.sorted(by: {$0.index < $1.index})) { weekday in

    VStack {
    
        Text(weekday.name)
    
        ForEach(weekday.shifts) { shift in
            HStack {
                DatePicker("Start: ", selection: shift.startTime, displayedComponents: .hourAndMinute)
                DatePicker("End: ", selection: shift.endTime, displayedComponents: .hourAndMinute)
            }
        }

    }
}

However, with the above code, I get the following errors: Cannot convert value of type '[Shift]' to expected argument type 'Binding<C>' and Generic parameter 'C' could not be inferred.

I then changed the overarching forEach to ForEach($availabilityManager.selectedWeekdays...

However, that gives the error Referencing operator function '<' on 'Comparable' requires that 'Binding<Int>' conform to 'Comparable'

Any help is appreciated :)

EDIT: These values are preset in the AvailablityManager class. The method provided does not allow me to change the values of startTime and endTime

    @Published var weekdays: [Weekday] = [
        Weekday(name: "Sunday", index: 0, shifts: [Shift(startTime: Calendar.current.date(bySettingHour: 8, minute: 0, second: 0, of: Date()) ?? Date(), endTime: Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date()) ?? Date())]),
        Weekday(name: "Monday", index: 1, shifts: [Shift(startTime: Calendar.current.date(bySettingHour: 8, minute: 0, second: 0, of: Date()) ?? Date(), endTime: Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date()) ?? Date())]),
        Weekday(name: "Tuesday", index: 2, shifts: [Shift(startTime: Calendar.current.date(bySettingHour: 8, minute: 0, second: 0, of: Date()) ?? Date(), endTime: Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date()) ?? Date())]),
        Weekday(name: "Wednesday", index: 3, shifts: [Shift(startTime: Calendar.current.date(bySettingHour: 8, minute: 0, second: 0, of: Date()) ?? Date(), endTime: Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date()) ?? Date())]),
        Weekday(name: "Thursday", index: 4, shifts: [Shift(startTime: Calendar.current.date(bySettingHour: 8, minute: 0, second: 0, of: Date()) ?? Date(), endTime: Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date()) ?? Date())]),
        Weekday(name: "Friday", index: 5, shifts: [Shift(startTime: Calendar.current.date(bySettingHour: 8, minute: 0, second: 0, of: Date()) ?? Date(), endTime: Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date()) ?? Date())]),
        Weekday(name: "Saturday", index: 6, shifts: [Shift(startTime: Calendar.current.date(bySettingHour: 8, minute: 0, second: 0, of: Date()) ?? Date(), endTime: Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date()) ?? Date())])
    ]

CodePudding user response:

To work with value in case of ForEach with Binding use wrappedValue accessor, like

ForEach($availabilityManager.selectedWeekdays.sorted(by: {$0.wrappedValue.index < $1.wrappedValue.index})) { weekday in
    VStack {
        Text(weekday.wrappedValue.name)
        ForEach(weekday.shifts) { shift in
            HStack {
                DatePicker("Start: ", selection: shift.startTime, displayedComponents: .hourAndMinute)
                DatePicker("End: ", selection: shift.endTime, displayedComponents: .hourAndMinute)
            }
        }
    }
}

Tested with Xcode 13.4 / iOS 15.5

  • Related