gpt4 book ai didi

ios - 预期返回“UITableViewCell”的函数中缺少返回值

转载 作者:行者123 更新时间:2023-11-28 10:10:41 25 4
gpt4 key购买 nike

为什么会出现此错误?Switch 语句不返回单元格?有人可以解释一下吗?

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

switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "LogoStyle1", for: indexPath) as! LogoStyle1
cell.backgroundColor = UIColor.clear
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "FieldStyle1", for: indexPath) as! FieldStyle1
cell.backgroundColor = UIColor.clear
cell.delegate = self
return cell
case 2:
let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonStyle1", for: indexPath) as! ButtonStyle1
cell.backgroundColor = UIColor.clear
cell.delegate = self
return cell
default:
break
}
}

最佳答案

这是编译器过度担心的事情之一。编译器基本上是在问你,如果 indexPath.row 既不是 0 也不是 1 也不是 2 怎么办?在那种情况下返回什么。作为程序员,您知道 indexPath.row 将始终是这些值之一,但编译器并不知道这一点。

所以你需要在 default 情况下返回一些东西,但是返回任何东西没有意义,不是吗?在这种情况下,我会写:

fatalError("Somehow indexPath.row is an unexpected value.")

如果达到 default 情况,这将使应用程序崩溃。如果达到默认情况,则可能意味着您的代码有问题(或者可能是 API 中的错误(不太可能)!),因为如您所知, TableView 只有 3 行。此时应用程序可能会崩溃。

关于ios - 预期返回“UITableViewCell”的函数中缺少返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48627386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com