Delete Data from Coredata Swift

后端 未结 8 1267
你的背包
你的背包 2020-12-05 00:51

In my tableViewController I have the following. And I am trying to get delete an item to work.

var myData: Array = []

override func tableV         


        
8条回答
  •  悲&欢浪女
    2020-12-05 01:19

    let entityDescription =
        NSEntityDescription.entity(forEntityName: "Contacts",
                                   in: managedObjectContext)
    let request: NSFetchRequest = Contacts.fetchRequest()
    request.entity = entityDescription
    if let result = try? managedObjectContext.fetch(request) {
        for object in result {
            do {
                try managedObjectContext.delete(object)
                txtName?.text = ""
                txtAddress?.text = ""
                txtPhone?.text = ""
                Status?.text = "Contact Deleted"
                txtName?.text = ""
    
            }
            catch let error {
                Status?.text = error.localizedDescription
            }
        }
    }
    

提交回复
热议问题