Search code examples
iosswiftuigesturerecognizerswipeunrecognized-selector

Swift Swipe gesture recognizer crashes program


Hello I added a swift gesture recognizer and it keeps crashing my program

override func viewDidLoad() {
    super.viewDidLoad()

    let leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("handleSwipes:")))
    let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("handleSwipes:")))

    leftSwipe.direction = .left
    rightSwipe.direction = .right

    self.view.addGestureRecognizer(leftSwipe)
    self.view.addGestureRecognizer(rightSwipe)
}

func handleSwipes(sender: UISwipeGestureRecognizer) {
    if (sender.direction == .left ) {
        headerLabel.text = "Zodiac Yearly"
    }

    if (sender.direction == .right ) {
        headerLabel.text = "Characteristics"
    }
}

This is the error I am getting

2017-09-08 20:17:33.355 Views[40942:2309262] -[Views.ViewController handleSwipes:]: unrecognized selector sent to instance 0x7fdd9bc0dc90
2017-09-08 20:17:33.361 Views[40942:2309262] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Views.ViewController handleSwipes:]: unrecognized selector sent to instance 0x7fdd9bc0dc90'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000111f40b0b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010f1e6141 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000111fb0134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x0000000111ec7840 ___forwarding___ + 1024
    4   CoreFoundation                      0x0000000111ec73b8 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010fbecf59 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
    6   UIKit                               0x000000010fbf4d57 _UIGestureRecognizerSendTargetActions + 109
    7   UIKit                               0x000000010fbf270b _UIGestureRecognizerSendActions + 225
    8   UIKit                               0x000000010fbf19ce -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 981
    9   UIKit                               0x000000010fbde152 _UIGestureEnvironmentUpdate + 1219
    10  UIKit                               0x000000010fbddc43 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 484
    11  UIKit                               0x000000010fbdce0a -[UIGestureEnvironment _updateGesturesForEvent:window:] + 274
    12  UIKit                               0x000000010f728eea -[UIWindow sendEvent:] + 4092
    13  UIKit                               0x000000010f6d5a84 -[UIApplication sendEvent:] + 352
    14  UIKit                               0x000000010feb95d4 __dispatchPreprocessedEventFromEventQueue + 2926
    15  UIKit                               0x000000010feb1532 __handleEventQueue + 1122
    16  CoreFoundation                      0x0000000111ee6c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    17  CoreFoundation                      0x0000000111ecc0cf __CFRunLoopDoSources0 + 527
    18  CoreFoundation                      0x0000000111ecb5ff __CFRunLoopRun + 911
    19  CoreFoundation                      0x0000000111ecb016 CFRunLoopRunSpecific + 406
    20  GraphicsServices                    0x0000000113ed7a24 GSEventRunModal + 62
    21  UIKit                               0x000000010f6b8134 UIApplicationMain + 159
    22  Views                               0x000000010ec06a67 main + 55
    23  libdyld.dylib                       0x0000000112f6c65d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Solution

  • let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(sender:)))
    
    @objc func handleSwipes(sender: UISwipeGestureRecognizer) {
        // ...
    }
    

    This should work in Swift 3 and 4.