NSInvalidArgumentException异常

异常:

2015-07-15 11:14:25.723 MeApp[12999:808613] -[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x7fe9b5d831e0
2015-07-15 11:14:25.729 MeApp[12999:808613] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x7fe9b5d831e0'

原因

问题出在为一个 ImageView 增加点击手势的地方

var deviceImagTapGesture = UITapGestureRecognizer()
        cell.deviceImage.tag = indexPath.row
        deviceImagTapGesture.addTarget(self, action: "deviceImagTaped:")
        cell.deviceImage.userInteractionEnabled = true
        cell.deviceImage.addGestureRecognizer(deviceImagTapGesture)

这里是回调的函数deviceImagTaped

// 点击设备图片触发设备控制动作
func deviceImagTaped(sender: UIImageView) {
        currentIndex = sender.tag
}

解决

问题出在 func deviceImagTaped(sender: UIImageView)
这里的参数 sender 不应该是 ImageView, 而是 UITapGestureRecognizer,当手势触发时, 这个UITapGestureRecognizer就是当前 cell, 也就是点的那个 ImageView.

func deviceImagTaped(sender: UITapGestureRecognizer) {
    currentIndex = sender.view!.tag
}

2015-09-10更新:
还有一种可能会导致这个错误, 应该还是没有访问到这个方法的原因.
private func deviceImagTaped(sender: UIImageView)
private 的访问权限导致报错

2015-07-15 12:011