swift Non-void function should return a value in a guard let

Viewed 5588
    guard let cell = tableView
        .dequeueReusableCell(withIdentifier: cellIdentifier) as? FolderAndFileCell else {
        print("some")
        return
    }

It says that

Non-void function should return a value

What should I return here?

2 Answers

Inside cellForRowAt you have to

guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? FolderAndFileCell else {
    print("some")
    return UITableViewCell()
}

This signature

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

should has a non void return value


The well-know approach is

 let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! FolderAndFileCell

This is an instance where you should be doing this instead.

 let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! FolderAndFileCell

You shouldn't need to use guard because I assume you are wanting to set the cell to FolderAndFileCell. If you can't set the cell to that, and go ahead and return a UITableViewCell() you are going to just get an empty cell and not know the reasoning.

I suggest you force cast to FolderAndFileCell and deal with the error if it presents then simply returning an empty cell if an error setting the cell is present.

Related