我试图让Delete按钮从我的核心数据堆栈中删除一些元素。我不确定我要去哪里,该功能应该删除术语和定义,但均不会删除。
@IBAction func deletePressed(_ sender: Any) {
do {
fetched = try context.fetch(Card.fetchRequest())
for object in fetched {
context.delete(object)
}
} catch {
print("Error")
}
ad.saveContext()
fetchData()
}
func fetchData() {
do {
fetched = try context.fetch(Card.fetchRequest())
for each in fetched {
termLabel.text = each.term!
definitionLabel.text = each.definition!
fetchedTerm.append(each.term!)
fetchedDefinition.append(each.definition!)
}
}
答案 0 :(得分:-1)
在(object as NSManagedObject)
函数中广播context.delete()
。