UITableView 转换为 Swift 时出错 3
UITableView error when converting to Swift 3
将旧应用程序从 swift 2.2 更新到 swift 4。我必须使用 swift 3 作为垫脚石。我转换为 3 但遇到以下错误:
Binary operator '==' cannot be applied to operands of type 'IndexPath' and '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",但我认为我的做法不对。
indexPath
包含 row
和 section
你需要指定你想要indexPath.row
还是indexPath.section
如果您的意思是 indexPath.row
,您的完整代码应该如下所示:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 || indexPath.row == 1 {
self.performSegue(withIdentifier: "NFL", sender: self)
}
if indexPath.row == 1 {
self.performSegue(withIdentifier: "AFL", sender: self)
}
if indexPath.row == 2 {
self.performSegue(withIdentifier: "FAI", sender: self)
}
if indexPath.row == 3 {
self.performSegue(withIdentifier: "IPA", sender: self)
}
}
提示:
您不需要转换为 NSIndexPath
出于此比较目的,您应该使用 switch
语句而不是许多 if
语句。
如果indexPath.row == 1
它将执行两次不同的结果。
因为 indexPath return IndexPath
in Swift 4 和 (indexPath as NSIndexPath).row
returning Int
所以你不能强制等于 。此外,您甚至不使用像 indexPath as NSIndexPath
这样的索引路径。 indexPath.row
够了
将旧应用程序从 swift 2.2 更新到 swift 4。我必须使用 swift 3 作为垫脚石。我转换为 3 但遇到以下错误:
Binary operator '==' cannot be applied to operands of type 'IndexPath' and '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",但我认为我的做法不对。
indexPath
包含 row
和 section
你需要指定你想要indexPath.row
还是indexPath.section
如果您的意思是 indexPath.row
,您的完整代码应该如下所示:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 || indexPath.row == 1 {
self.performSegue(withIdentifier: "NFL", sender: self)
}
if indexPath.row == 1 {
self.performSegue(withIdentifier: "AFL", sender: self)
}
if indexPath.row == 2 {
self.performSegue(withIdentifier: "FAI", sender: self)
}
if indexPath.row == 3 {
self.performSegue(withIdentifier: "IPA", sender: self)
}
}
提示:
您不需要转换为 NSIndexPath
出于此比较目的,您应该使用 switch
语句而不是许多 if
语句。
如果indexPath.row == 1
它将执行两次不同的结果。
因为 indexPath return IndexPath
in Swift 4 和 (indexPath as NSIndexPath).row
returning Int
所以你不能强制等于 。此外,您甚至不使用像 indexPath as NSIndexPath
这样的索引路径。 indexPath.row
够了