我在尝试设置indexPath时遇到错误:条件绑定(bind)的初始化程序必须具有可选类型
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?) {
if segue.identifier == "toStation" {
let detailViewController = segue.destinationViewController
as! toStation
if let indexPath = tableView.indexPathForSelectedRow! {
detailViewController.selectedStation = stationNameArray[indexPath.row]
}
}
}
最佳答案
if let indexPath = tableView.indexPathForSelectedRow!
您将indexPathForSelectedRow 绑定(bind)到indexPath View ,使用if let 语句来解包可选的indexPathForSelectedRow。这是一个好方法。但同时你正在使用 bang 语句!打开可选的包装。实际上,您将可选内容展开两次。要么用!或 if let 语句。
关于swift - 如何在 TableView 中使用prepareForSegue 传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35540641/