Group Group Group Group Group Group Group Group Group

Newbie Help Please!

I seem to be going round in circles with Core Data in Xcode/SwiftUI. I have entered the code (and have got running) Both “Chapter 1: Your first Core Data App” through to “Chapter 3: The Core Data Stack”. I am beginning to understand the code, but am having difficulties with my own modification of the ‘HitList’ code in Chapter 1. My problem is in setting up a Core Data ‘Delete’ option in the ‘HitList’ example similar to the one in the ‘DogWalk’ example. Regretfully, I am getting a bit lost in comparing the the examples. Can anyone help in providing a method of setting up the ‘Delete’ in the ‘HitList’ example?
Thanks in advance!

@pilotsoft Thanks very much for your question!

Can you post the specific block of code that you are using, and is causing you trouble? Can you also provide the error message that you’re getting?

I am very embarrassed about my very poor knowledge of object oriented code, my background is PASCAL, C and Clarion on PC’s. (Have now fully committed to Apple and am trying to get a handle of things. As a trial, I have developed a couple of small projects using arrays etc. but with limited data persistence. I purchased the ‘Core Data by Tutorials’ book and am working my way through the examples. In the process, I am trying to use a simple one cell data array very similar to the first example in the book, referencing the ‘Bow Tie’ example which does have a ‘delete’ feature, but I can not get my head around the differences between the two examples with regard to the methods used. The nearest I have come to getting code to be compilable is as below. Everything works until I actually press ‘Delete’ when it then crashes. I do know that my ‘code’ is inadequate and needs reference to the array, but not sure how to achieve that in the function.

func tableView(_ tableView: UITableView,
canEditRowAt indexPath: IndexPath) -> Bool {
return true

func tableView(_ tableView: UITableView,
               commit editingStyle: UITableViewCell.EditingStyle,
               forRowAt indexPath: IndexPath) {

 tableView.deleteRows(at: [indexPath], with: .automatic
  tableView.deleteRows(at: [indexPath], with: .automatic)

Hi @pilotsoft, no need to be embarrassed here! We are all learners and it’s awesome to hear that you’re going thru the Core Data book. I’ve added a small change to your code below. Hope this helps.

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
    objects.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .fade)

Thank you so much for your help, your input works perfectly (with suitable change from ‘object’ to ‘people’ of course!)

With your suggestion, have followed through on my abysmal swift knowledge and a dim light is starting to register in the deepest, darkest recesses of what passes for my mind!

Can’t tell you how much I appreciate the response, was not really expecting one at all! Quite raises my (admittedly limited) faith in humanity!

1 Like