Swift: Gesture Recognizer unrecognized selector se

2019-02-24 11:09发布

问题:

I'm attempting to make a gesture recognizer in XCode, so that I can tap on my MKMapView and preform some actions. However, I am receiving the "unrecognized selector sent to instance" whenever I long-press the map.

Here is my code in viewDidLoad:

let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleTap:");
self.mapViewPlace.addGestureRecognizer(gestureRecognizer);

And here is the function later on:

func handleTap(gestureReconizer: UILongPressGestureRecognizer) {

}

Any ideas?

回答1:

Please, give Neo credit. You need to change your syntax to this:

let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleTap)

Side note: For Swift you do not need semi-colons the end your code lines.