我试图查看用户是否在表视图中按下单元格,但我无法将其与nil进行比较?我如何检查是否没有按下一个单元格?当用户长按表格视图
时,这是处理程序func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
let p: CGPoint = gestureRecognizer.locationInView(self.tableView)
let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(p)!
if indexPath == nil{
}
else if gestureRecognizer.state == .Began {
print(indexPath.row)
}
}
答案 0 :(得分:4)
您不应该强制解包,然后检查nil
- 这会导致您的应用崩溃。你应该做的是:放下展开然后进行比较!
func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
let point = gestureRecognizer.locationInView(self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(point)
if let touchedPath = indexPath {
if gestureRecognizer.state == .Began {
print(touchedPath.row)
}
}
}