如何快速删除核心数据中的实体?

时间:2019-06-07 02:42:59

标签: ios swift core-data

我试图让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!)
    }
  }

1 个答案:

答案 0 :(得分:-1)

(object as NSManagedObject)函数中广播context.delete()