0

正在使用 Xcode 7 和 swift 2。应用程序运行良好。更新到 Xcode 8。它自动从 swift 2 转换代码 --> swift 3。现在我的表格视图的代码有问题。

错误在于这行代码:

if (indexPath as NSIndexPath).row == 0 || indexPath == 1 {
        counter = 0
        self.performSegue(withIdentifier: "Day1", sender: self)
}

正如它所说,二元运算符'=='不能应用于操作数类型'索引路径'和'int'

这是什么意思,我该如何解决?

   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if (indexPath as NSIndexPath).row == 0 || indexPath == 1 {
        counter = 0
        self.performSegue(withIdentifier: "Day1", sender: self)
    }

    if (indexPath as NSIndexPath).row == 1 {
        counter = 1
        self.performSegue(withIdentifier: "Day2", sender: self)
    }
}
4

1 回答 1

2

错误来自这段代码

indexPath == 1

你需要得到它的row类型Int

indexPath.row == 1

另请注意,无需转换IndexPathNSIndexPath

indexPath.row

那么我认为您可能不想在第一个 if 语句中检查第二个条件,因为在这种情况下,第二个 if 语句将不会按照您的意愿执行

if indexPath.row == 0

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == 0 {
        counter = 0
        self.performSegue(withIdentifier: "Day1", sender: self)
    } else if indexPath.row == 1 {
        counter = 1
        self.performSegue(withIdentifier: "Day2", sender: self)
    }
}
于 2019-01-09T13:11:16.363 回答