Gesture throwing unrecognized selector error

时间:2017-03-22 18:43:58

标签: ios swift xcode swift3

I'm trying to rewrite this line in Swift 3 Swift 2: let longPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressDetected")

Swift 3:

let longPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("longPressDetected")))

but it seems to be throwing this error

unrecognized selector sent to instance

The function that is's trying to call is this:

func longPressDetected(_ sender: Any){}

1 个答案:

答案 0 :(得分:1)

The corresponding selector for

func longPressDetected(_ sender: Any){}

in Swift 3 is

#selector(longPressDetected(_:))

or even simply

#selector(longPressDetected)

Side-note: Since the sender is a distinct type you should specify this:

func longPressDetected(_ sender: UILongPressGestureRecognizer){}

and don't annotate types the compiler can infer.