转换为Swift 3时出现UITableView错误

转换为Swift 3时出现UITableView错误,swift,uitableview,swift3,swift2,Swift,Uitableview,Swift3,Swift2,将旧应用程序从swift 2.2更新为swift 4。我必须用swift 3作为垫脚石。我转换为3,但遇到以下错误: 二进制运算符“==”不能应用于“IndexPath”和“Int”类型的操作数` 代码是: override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if (indexPath as NSIndexPath).row == 0 || indexPath =

将旧应用程序从swift 2.2更新为swift 4。我必须用swift 3作为垫脚石。我转换为3,但遇到以下错误:

二进制运算符“==”不能应用于“IndexPath”和“Int”类型的操作数`

代码是:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if (indexPath as NSIndexPath).row == 0 || indexPath == 1 {
        self.performSegue(withIdentifier: "NFL", sender: self)
    }

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

    if (indexPath as NSIndexPath).row == 2 {
        self.performSegue(withIdentifier: "FAI", sender: self)
    }

    if (indexPath as NSIndexPath).row == 3 {
        self.performSegue(withIdentifier: "IPA", sender: self)
    }
}

为什么我在Swift 3中得到这个错误而不是2.2?我试图将其强制为“Int”,但我认为我的方法不对。

indepath
包含

您需要指定是想要
indexPath.row
还是
indexPath.section

如果您实际上指的是
indexPath.row
,那么您的完整代码应该如下所示:

override func tableView(tableView:UITableView,didSelectRowAt indepath:indepath){
如果indexPath.row==0 | | indexPath.row==1{
self.performsgue(带有标识符:“NFL”,发送方:self)
}
如果indexath.row==1{
self.performsgue(带有标识符:“AFL”,发送方:self)
}
如果indexath.row==2{
self.performsgue(带有标识符:“FAI”,发送者:self)
}
如果indexath.row==3{
self.performsgue(标识符为“IPA”,发送方为self)
}
}

提示:

您不需要强制转换到
nsindepath

为了进行比较,您应该使用
switch
语句,而不是许多
if
语句


如果
indexath.row==1
它将执行两次检查,结果不同。

因为在Swift 4和
中indexath返回
indexath
(indexath作为nsindexath)。row
返回
Int
,所以不能强制相等。而且你甚至不需要像
indexath那样使用indexath作为nsindexath
indexPath.row
就足够了

短语
indexPath==1
也不会在Swift 2.2中编译,因此您在这里并没有真正告诉我们真相。这个答案中的逻辑(可能还有原始代码)有一个很大的缺陷。如果行为1,则第二个
if
将导致执行两个步骤。在这里使用
if-else
会更好。