FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

Project 5 (UIKit): NSGestureRecognizer doesn't fire the action (mapClicked)

Forums > macOS


Hi! environment is xcode 12.2/Catalina (10.15.7), and I'm trying to get pins to drop on the map at the start of the project (and nothing happens)

Some background: I haven't had to get a developer account - the error and capabilities requirement mentioned on page 254 ("enabling maps") didn't generate an error, and the map renders fine.

Using my relatively amateur debugging skills, it looks like the NSGestureRecognizer registers agains the view fine:

    override func viewDidLoad() {

        let recognizer = NSGestureRecognizer(target: self, action: #selector(mapClicked))

but no gesture is ever recognised (it never calls mapClicked)

    @objc func mapClicked(recognizer: NSClickGestureRecognizer) {
        let location = recognizer.location(in: mapView)
        let coordinates = mapView.convert(location, toCoordinateFrom: mapView)
        addPin(at: coordinates)

Any thought on whether Is this related to signing/the maps capability? (I wasn't going to spend £79 to find out, but I guess I'm going to need an account at some point :) update: did it, looks like the capabilities UI has changed, but figured out how to add it (plus sign on the top left) and the capability is now listed in the entitlements - still doesn't trigger mapClicked

Is there a more effective way to debug NSGestureRecognizer?



solved: NSGestureRecognizer should be NSClickGestureRecognizer

doh :/


Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.