Group Group Group Group Group Group Group Group Group

Chapter 19: Unexpectedly found nil

I’m getting an error in iOS Apprentice 8.3.0 Chapter 19.

Here is the code in AllListsViewController.swift:

    let cell: UITableViewCell!
     if let c = tableView.dequeueReusableCell(
        withIdentifier: cellIdentifier) {
      cell = c
     } else {
      cell = UITableViewCell(style: .subtitle,
                reuseIdentifier: cellIdentifier)
     }
    
    // Update cell information
    let checklist = dataModel.lists[indexPath.row]
    cell.textLabel!.text = checklist.name
    cell.accessoryType = .detailDisclosureButton
    let temp = checklist.countUncheckedItems()
    cell.detailTextLabel!.text =
           "\(checklist.countUncheckedItems()) Remaining"
    return cell

The errror is on: cell.detailTextLabel!.text = “(checklist.countUncheckedItems()) Remaining”. Error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I can get around the error by commenting out the code around dequeueReusbleCell so that it only executes the UITableViewCell. But I’d like to understand why this is happening. It happens when the code is first started and I’ve verified that the dequeue code is executing even though this is the first cell being created for the checklist array. I initially thought that it was related to the creating the default list. I commented that out and am still getting the error.

Any suggestions would be helpful.

Never mind. I missed the paragraph that told me to remove the register(_:forCellReuseIdentifier:).

My apologies.

Cool, problem resolved.?

Yes, problem resolved .