How do I list the relationships of a particular entity that is fetched?
I know how to list the entries for the entity itself, but I can't figure out how to list one-to-many relationships in a List.
See this code:
struct TodoItemView: View {
var todoList: TodoList
var body: some View {
VStack {
Text(todoList.title!)
List {
ForEach(todoList.todoItems, id: \.self) {todoItem in
Text(todoItem.title!)
}
}
}
}
}
That is producing errors and I have no clue how to fix them:
- Protocol type 'NSSet.Element' (aka 'Any') cannot conform to 'Hashable' because only concrete types can conform to protocols
- Value of type 'NSSet.Element' (aka 'Any') has no member 'title'
I've set up my TodoList entity to have a one-to-many with TodoITem. I fetch the todoList in the previous view and then pass it to this view:
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(
entity: TodoList.entity(),
sortDescriptors: [NSSortDescriptor(key: "order", ascending: true)]
) var todoLists: FetchedResults<TodoList>
// ...more code here
List {
ForEach(todoLists, id: \.self) {todoList in
NavigationLink(destination: TodoItemView(todoList: todoList), label: {
Text(todoList.title!).foregroundColor(stringToColor(string: todoList.color!))
})
}
}
How do I use todoList.todoItems in a ForEach loop the same way so that I can also run CRUD operations on it as well?