title: How do I delete an object for CoreData in Swift 4
body: <p>So I've written some code to read information from CoreData.</p>
  
  <pre><code>func getData() {        
      let appDelegate = UIApplication.shared.delegate as! AppDelegate
      let context = appDelegate.persistentContainer.viewContext
      // let newSitting = NSEntityDescription.insertNewObject(forEntityName: "Sittings", into: context)
  
  
      //get data from CoreData
      let request = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "Sittings")
  
      request.returnsObjectsAsFaults = false
  
      do {
          let results = try context.fetch(request)
  
          if results.count &gt; 0 {
              for result in results as! [NSManagedObject] {
                  if let sittingDate = result.value(forKey: "date") as? Date {                        
                      //add data to array
                      sittingsDatesArr.append(sittingDate)                        
                  }                    
              }
          }
          else {
              print ("database is empty")
          }
      }
      catch {
          print ("error")
      }
  }
  </code></pre>
  
  <p>And write data to CoreData,</p>
  
  <pre><code>func writeData(sitDate: Date) {
      // setup CoreData
      let appDelegate = UIApplication.shared.delegate as! AppDelegate
      let context = appDelegate.persistentContainer.viewContext
      let newSitting = NSEntityDescription.insertNewObject(forEntityName: "Sittings", into: context)
  
      newSitting.setValue(sitDate, forKey: "date")
  
      do {
          try context.save()
          print ("-----SAVED-----")
      }
      catch {
          print ("XXXXX THERE WAS AN ERROR XXXXXXX")
      }
  }
  </code></pre>
  
  <p>and all that seem to work OK.</p>
  
  <p>But I can't seem to work out how to delete an entry when the user slides to delete the row in the UITableView.
  So far I have this,</p>
  
  <pre><code>    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
          if (editingStyle == UITableViewCellEditingStyle.delete) {               
              dateFormatter.dateStyle = .long
              dateFormatter.timeStyle = .long
  
              // remove item from array
              sittingsDatesArr.remove(at: indexPath.row)
  
              // remove item from core data
  
  
              //refresh
              refresh()
          }
      }
  </code></pre>
  
  <p>For reference, this is the refresh function,</p>
  
  <pre><code>@objc func refresh(){
      getData()
      print("refreshed") 
      viewTable.reloadData()
  }
  </code></pre>