I am really new to SectionedFetchRequest. I was trying to group results in my CoreData by a dateString that is in each item and the searched I did showed that SectionedFetchRequest was the best method for accomplishing this. However, whenever I try to compile I am getting an error saying...
Cannot convert value of type 'SectionedFetchResults<String, FoodItem>.Element' to expected argument type 'Binding< C >' generic parameter 'C' could not be inferred
I'm not sure what I am doing wrong as I followed the post I found completely. I'll post my code and then the link at the bottom... Here's my code...
struct SavedItemsView: View {
@Environment(\.managedObjectContext) var viewContext
@SectionedFetchRequest<String, FoodItem>(sectionIdentifier: \FoodItem.consumedDate, sortDescriptors: []) var grouped: SectionedFetchResults<String, FoodItem>
var body: some View {
List {
ForEach(grouped) { section in
Section(header: Text(section.id)) {
ForEach(section) { food in //THIS IS WHERE THE ERROR IS
HStack {
Text(food.name).font(.bodyLarge)
Text(food.dateString)
}
}
}
}
}
}
}
No idea why that foreach is causing the issue. that is exactly what it shows in the post, and every other link I could find when dealing with SectionedFetchRequest, they all show a List with 2 ForEach inside, one for the main results and one for each section in the results.
Not sure if it matters, but I can post up my FoodItem class...
FoodItem+CoreDataProperties.swift
extension FoodItem {
@nonobjc public class func fetchRequest() -> NSFetchRequest<FoodItem> {
return NSFetchRequest<FoodItem>(entityName: "FoodItem")
}
@NSManaged public var name: String?
@NSManaged public var mealType: String?
@NSNanaged public var dateString: String?
@NSManaged public var timestamp: Int64
@NSManaged public var calories: Int16
}
extension FoodItem: Identifiable {
}
extension FoodItem {
@objc var consumedDate: String {
dateString ?? Date().formatted(date: .abbreviated, time: .omitted)
}
}
Here's the link... https://www.hackingwithswift.com/forums/swiftui/grouping-atfetchrequest-by-date-and-display-results-in-navigationview/15767
This error is usually because of a typo. In your case there are 2 obvious ones.
Text(food.name).font(.bodyLarge)
Text(food.dateString)
You should be able to fix it by
Text(food.name ?? “No name”).font(.bodyLarge)
Text(food.dateString ?? “No date”)
You can comment out sections of code to narrow down the problematic section.