Implement Unwrapped Cells for Improved Type Safety#1382
Implement Unwrapped Cells for Improved Type Safety#1382gokulvenkat243 wants to merge 3 commits intomastodon:developfrom
Conversation
|
Thank you for noticing this. Please resubmit with the following change: rather than fatalError(), which will crash in production, return nil when the cell is nil or of an unexpected type: |
| switch item { | ||
| case .header(let headerContext): | ||
| let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ReportHeadlineTableViewCell.self), for: indexPath) as! ReportHeadlineTableViewCell | ||
| guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ReportHeadlineTableViewCell.self), for: indexPath) as? ReportHeadlineTableViewCell else { fatalError("WTF?! Wrong cell.") } |
There was a problem hiding this comment.
To avoid crashing in production, please use assertionFailure("unexpected cell dequeued") and then return nil.
whattherestimefor
left a comment
There was a problem hiding this comment.
Thank you for making these changes. A couple edits to the spots that aren't quite the same as the others and then this should be good to merge.
| guard let cell: PollOptionTableViewCell = { | ||
| let _cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PollOptionTableViewCell.self) + "@\(indexPath.row)#\(indexPath.section)") as? PollOptionTableViewCell | ||
| _cell?.prepareForReuse() | ||
| return _cell ?? PollOptionTableViewCell() |
There was a problem hiding this comment.
This block will never return nil (if _cell is nil, it will create a new PollOptionTableViewCell()), so the else will never be reached. Best fix would be to remove the ?? PollOptionTableViewCell(), so that the block can return nil and the new else can then be reached.
| guard let cell: PollOptionTableViewCell = { | ||
| let _cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PollOptionTableViewCell.self) + "@\(indexPath.row)#\(indexPath.section)") as? PollOptionTableViewCell | ||
| _cell?.prepareForReuse() | ||
| return _cell ?? PollOptionTableViewCell() |
| parameters.visiblePath = UIBezierPath(roundedRect: mediaView.bounds, cornerRadius: MediaView.cornerRadius) | ||
| return UITargetedPreview(view: mediaView, parameters: parameters) | ||
| } else { | ||
| assertionFailure("unexpected cell dequeued") |
There was a problem hiding this comment.
This is not a dequeued cell being provided to populate the tableview, but rather a cell that is already in the tableview and is being accessed to learn things about its contents. The assertion could be something like "unexpected cell type at (indexPath)"
Issue: #1381
Description: