简体   繁体   中英

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

    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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM