Checklists bonus exercise Chapter 20

Has anyone done these bonus objectives at the end of Chapter 20? I’m trying to sort the checklist items by due date but I cannot figure it out. I created another DataModel function that is almost identical to the sortChecklists() but it appears it is causing my dataModel var to be nil somehow. I think. I don’t really know how to debug very well so I could be wrong.

added:

var listItems = [ChecklistItem]()

func sortChecklistItems() {
    listItems.sort(by: { checklistItem1, checklistItem2 in return checklistItem1.dueDate.compare(checklistItem2.dueDate) == .orderedAscending})

I then call sortChecklistItems() in the ChecklistViewController’s didFinishAdding & didFinishEditing functions right after the .append line and added:

var dataModel: DataModel!

It’s a bit difficult to see what is going on based on the code snippets you provided :slight_smile: If you can upload your project as a ZIP file somewhere and post a link here, I can take a look and tell you what might be going on.

Your issue is in ChecklistViewController where you define a new dataModel variable on line 52 but I don’t believe you initialize it anywhere :slight_smile:

If you check, for AllListsViewController you pass the data model via AppDelegate.swift on line 22.

Similarly, when you create a new instance of ChecklistViewController, you need to pass it the dataModel from AllListsViewController. The best place to do this is probably in AllListsViewController.swift when you prepare for segue. You need to pass the dataModel to ChecklistViewController there. Otherwise, your dataModel variable in ChecklistViewController would be nil and would cause the crash you’re seeing.

I added the same line from the AppDelegate to the AllListViewController’s prepareForSegue:

controller.dataModel = dataModel

but now the AppDelegate has a SIGABRT crash. I am very confused.

If you look at the Console in Xcode, you should see the error:

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

You do need to look at the Console messages rather than what Xcode shows in the source window since sometimes even though the crash is shown in the AppDelegate, that’s not where the issue is. The issue is actually with these rows (line 26 - 28 in ChecklistViewController.swift):

let indexPath = IndexPath(row: newRowIndex, section: 0)
let indexPaths = [indexPath]
tableView.insertRows(at: indexPaths, with: .automatic)

You reload the tableview (which loads all available data) and then try to insert a new row into the table too. Since you’ve already got all the data (including the new row) displaying, trying to insert a new row crashes the app.

If you encounter further crashes though, I’m afraid that is beyond the scope of the book to assist you with since this is new code that you’ve added :slight_smile:

fadewave

I’ve been trying to do the challenge and the same problem occurred to me. To, at least, make things works… I created a sortChecklistItems() in CheckListViewController and called it in viewWillAppear() together with tableView.reloadData()

1 Like

I found that the simplest solution would be to add the sort method to the Checklist class since it owns the list of items. Then it was easy to perform the sorting in the ChecklistViewController since I already had a checklist property there.

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.