Home > OS >  What action when tableview can't dequeue a cell
What action when tableview can't dequeue a cell

Time:09-17

When I was learning about tableviews the tutorials I followed used the following code-

guard let cell = tableView.dequeueReusableCell(withIdentifier: Self.reminderListCellIdentifier, for: indexPath) as? ReminderListCell else {
        return UITableViewCell()
    }

I just came across example code from Apple which is;

guard let cell = tableView.dequeueReusableCell(withIdentifier: Self.reminderListCellIdentifier, for: indexPath) as? ReminderListCell else {
        fatalError("Unable to dequeue ReminderCell")
    }

What should I implement? fatalError causes a crash I believe. Is the the desired behaviour?

CodePudding user response:

None of the suggestions. Force unwrap the cell

let cell = tableView.dequeueReusableCell(withIdentifier: Self.reminderListCellIdentifier, for: indexPath) as! ReminderListCell

In practice it causes the same behavior as the fatalError. The code must not crash if everything is hooked up correctly. The potential mistake is a design mistake.

The first snippet is pretty silly because in case of the mentioned design mistake nothing will be displayed and you have no idea why.

Force unwrapping is not evil per se.

  • Related