Andrey Liberty Andrey Liberty - 7 months ago 18
Swift Question

GestureRecognizer: placing pin on Map results in SIGABRT

I'm trying to add a gesture recognizer to the map; but, when I launch the program, it crashes with the signal SIGABRT. Can you help me to figure it out because I'm really new to the iOS development?

override func viewDidLoad() {
super.viewDidLoad()
v.hidden=true
let lpgr = UILongPressGestureRecognizer(target: self, action:"handleLongPress:")
lpgr.delegate=self
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
self.theMap.addGestureRecognizer(lpgr)
}

func action(gestureRecognizer:UIGestureRecognizer){
var touchPoint = gestureRecognizer.locationInView(theMap)
var newCoordinates = theMap.convertPoint(touchPoint, toCoordinateFromView: theMap)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinates
theMap.addAnnotation(annotation)
}



2016-02-07 13:30:17.963 SM[25924:2977301] -[SM.ViewController3 handleLongPress:]: unrecognized selector sent to instance 0x7f882a66d4d0
2016-02-07 13:30:17.968 SM[25924:2977301] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SM.ViewController3 handleLongPress:]: unrecognized selector sent to instance 0x7f882a66d4d0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010b046e65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010d042deb objc_exception_throw + 48
2 CoreFoundation 0x000000010b04f48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010af9c90a ___forwarding___ + 970
4 CoreFoundation 0x000000010af9c4b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010c007e73 _UIGestureRecognizerSendTargetActions + 153
6 UIKit 0x000000010c0044e5 _UIGestureRecognizerSendActions + 162
7 UIKit 0x000000010c0024e2 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
8 UIKit 0x000000010c00a9a0 ___UIGestureRecognizerUpdate_block_invoke904 + 79
9 UIKit 0x000000010c00a83e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
10 UIKit 0x000000010bff8101 _UIGestureRecognizerUpdate + 2634
11 CoreFoundation 0x000000010af72367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
12 CoreFoundation 0x000000010af722d7 __CFRunLoopDoObservers + 391
13 CoreFoundation 0x000000010af67f2b __CFRunLoopRun + 1147
14 CoreFoundation 0x000000010af67828 CFRunLoopRunSpecific + 488
15 GraphicsServices 0x000000010e75cad2 GSEventRunModal + 161
16 UIKit 0x000000010bb1f610 UIApplicationMain + 171
17 SM 0x000000010add1a8d main + 109
18 libdyld.dylib 0x000000010f0b192d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)


The class itself is the delegated of the
GestureRecognizer
.

Answer

The func should be named handleLongPress instead of being named action:

I.e.,

func handleLongPress(gestureRecognizer:UIGestureRecognizer)
{
     var touchPoint = gestureRecognizer.locationInView(theMap)
     var newCoordinates = theMap.convertPoint(touchPoint, toCoordinateFromView: theMap)
     let annotation = MKPointAnnotation()
     annotation.coordinate = newCoordinates
     theMap.addAnnotation(annotation)
}