danieltj27
(Daniel James)
1
I'm trying to learn how to use Core Data and I have a few sub-views that I want to pass the collection of items from Core Data through but I can't get it to work and the error messages make no sense to me.
This is what I have in my (main) ContentView:
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.firstName, ascending: true)]) var people: FetchedResults<Item>
I then have the following code in my call to the sub-view:
PeopleListView(people: people)
Inside the PeopleListView I have this to try and bind the variable.
@Binding var people: FetchedResults<Item>
The error I'm getting is the call to the sub view which is this:
Cannot convert value of type 'FetchedResults<Item>' to expected argument type 'Binding<FetchedResults<Item>>'
Can someone explain this in really simple terms on why this isn't working and how I can fix it? I'm really struggling with Core Data, I feel like there's so much going on behind the scenes that I just don't understand.
Replace people with $people. The latter resolves to the expected type Binding<FetchedResults<Item>>.
I'm having a similar problem when compiling a macOS app on Xcode 15.2, on macOS 14.2.1.
A view with a FetchedResults binding:
struct CountView: View {
@Binding var items: FetchedResults<Item>
var body: some View {
Text("Number of items: \(items.count)")
}
}
In the main ContentView:
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
// ...
CountView(items: $items)
The compiler error:
Cannot convert value of type 'Binding<FetchRequest.Configuration>' to expected argument type 'Binding<FetchedResults>'
I don't understand why the type of $items is Binding<FetchRequest<Item>.Configuration>.
sed76
(Sed76)
4
Exactly the same problem & error message, xcode Version 15.2 (15C500b). Can anyone explain how to pass @FetchRequest'ed fetchedresults to subviews? I need to pass fetched results to filter-like view, so that original list could be dynamically modified from child (Filter view).