I am trying to set up a collapsible List
in my Core Data backed macOS app to allow navigation in the side bar.
Project
is the main entity and has a to-many relationship with File
.
It should look something like this:
Project 1 >
File1
File2
File3
Project 2 >
File21
File22
File23
Where the >
indicates it is collapsible.
The error I get is below the code:
public extension Project {
var filesArray: [File]? {
guard let set = files as? Set<File>, set.isEmpty == false else { return nil }
return Array(set)
}
}
struct SplitView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
entity: Project.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Project.name, ascending: true)]
) var projects: FetchedResults<Project>
var body: some View {
NavigationSplitView {
List(projects, children: \.filesArray) { file in // <<-- Error in this line
Text(file.name ?? "no name")
}
} detail: {
Text("Detail Area") // TO DO
}
}
I get the following error for the children property:
Key path value type '[File]?' cannot be converted to contextual type 'FetchedResults?'
How can I fix this?