I am wondering on how to delete a single object in core data. I have a vc with a label on, when a user swipes left or right it shows each input from core data. I want the user to be able to delete the selected input on the label without delete all of the other inputs.
Here is the code that I have tried below
This is where I swipe and left
@objc func handleSwipes(sender: UISwipeGestureRecognizer) {
if sender.direction == .left {
currentArrayIndex = (currentArrayIndex + 1) % coreNames.count
mainSubNameTF.leftToRightAnimation() // Swipe Animation from left to right for mainSubNameTF
} else if sender.direction == .right {
currentArrayIndex = (currentArrayIndex + coreNames.count - 1) % coreNames.count
mainSubNameTF.rightToLeftAnimation() // Swipe Animation from right to left for mainSubNameTF
}
mainSubNameTF.text = coreNames[currentArrayIndex].subName // Shows subName Core Data to mainSubNameTF
}
This is where i Delete the object
func deleteSub() {
do {
coreNames = try context.fetch(SubNames.fetchRequest())
for each in coreNames {
context.delete(each)
}
appDelegate.saveContext()
} catch {
}
}