0

I'm using "as!" for a custom tableView cell and my understanding is that it's generally OK, however I'm using SwiftLint and would like to see if I can provide an alternative. Using a guard statement, is there an alternative I can provide that won't cause a crash? Or should I go a different route? Thanks!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as? CustomTableViewCell
         else {
            return //something?
    } 
moosgrn
  • 379
  • 3
  • 13

2 Answers2

1

You could return a UITableViewCell with one of the styles UIKit provides for you.

For example UITableViewCell(style: .default, reuseIdentifier: "YourReuseIdentifier").

Maci
  • 123
  • 8
1

There is no alternative. Force unwrap the cell! That's one of the rare cases where force unwrapping is fine.

The code must not crash if the class of the cell and its identifier is set correctly.

I doubt the usefulness of those tools which only distinguish between black and white.

Returning something like a generic cell couldn't be a solution because it causes bad user experience, too.

vadian
  • 274,689
  • 30
  • 353
  • 361