I'm trying to get an index number of Coredata at the list.
Here is my current code
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: ToDoItem.getAllToDoItems()) var toDoItems:FetchedResults<ToDoItem>
@State private var newTask = ""
var body: some View {
VStack{
HStack{
TextField("new Task", text: $newTask)
Button(action:{
//saving event title
let toDoItem = ToDoItem(context: self.managedObjectContext)
toDoItem.title = self.newTask
do {
try self.managedObjectContext.save()
} catch {
print(error)
}
self.newTask = ""
})
{
Text("Add")
}
}
List{
ForEach(self.toDoItems) { toDoItem in
HStack{
Text("\(toDoItem.title!)")
}.onTapGesture {
print("Print current Index")
}
}
}
}
}
}
Here is what I have tried:
I tried to use solutions at here, but keep getting this error
Cannot convert value of type 'FetchedResults<ToDoItem>' to expected argument type 'Range<Int>'
So I tried to use a loop statement to check all CoreData elements until the same name is matched, but problem was user can have multiple CoreData items with same name.
Would be there anyway to get the index number of CoreData at List?