Search code examples
iosswiftunrecognized-selectoruiswipegesturerecognizer

iOS app crashing during swipe gesture?


Working with practice on swipe gestures for a developer course I am taking. I have followed the steps to the best of my knowledge but it seems that every time I execute a swipe (right or up), my app crashes.

I have searched for a solution but can not seem to find out why it is crashing. I have added my ":" to my action and also added UIGestureRecognizerDelegate to my classes.

The app will run, it just crashes when I execute a swipe.
Here is my code:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeUp = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeUp.direction = UISwipeGestureRecognizerDirection.Up
    self.view.addGestureRecognizer(swipeUp)


    func swiped(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {

            case UISwipeGestureRecognizerDirection.Right:
                print("swiped right")
            case UISwipeGestureRecognizerDirection.Up:
                print("swiped up")
            default:
                break

            }

        }

    }



}

The error message I receive is the following.

2015-10-26 10:59:52.944 Swipes and Shakes[2764:444281] -[Swipes_and_Shakes.ViewController swiped:]: unrecognized selector sent to instance 0x7fea2a58bcd0
2015-10-26 10:59:52.950 Swipes and Shakes[2764:444281] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swipes_and_Shakes.ViewController swiped:]: unrecognized selector sent to instance 0x7fea2a58bcd0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010dfb2f45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010fccedeb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010dfbb56d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010df08eea ___forwarding___ + 970
    4   CoreFoundation                      0x000000010df08a98 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010ecae37c _UIGestureRecognizerSendTargetActions + 153
    6   UIKit                               0x000000010ecaacf6 _UIGestureRecognizerSendActions + 162
    7   UIKit                               0x000000010eca8cf3 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
    8   UIKit                               0x000000010ecb0c9f ___UIGestureRecognizerUpdate_block_invoke877 + 79
    9   UIKit                               0x000000010ecb0b3d _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
    10  UIKit                               0x000000010ec9edb1 _UIGestureRecognizerUpdate + 2634
    11  UIKit                               0x000000010e840684 -[UIWindow _sendGesturesForEvent:] + 1137
    12  UIKit                               0x000000010e8418ba -[UIWindow sendEvent:] + 849
    13  UIKit                               0x000000010e7f101a -[UIApplication sendEvent:] + 263
    14  UIKit                               0x000000010e7cb8c7 _UIApplicationHandleEventQueue + 6844
    15  CoreFoundation                      0x000000010dedf011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x000000010ded4f3c __CFRunLoopDoSources0 + 556
    17  CoreFoundation                      0x000000010ded43f3 __CFRunLoopRun + 867
    18  CoreFoundation                      0x000000010ded3e08 CFRunLoopRunSpecific + 488
    19  GraphicsServices                    0x00000001125d7ad2 GSEventRunModal + 161
    20  UIKit                               0x000000010e7d1031 UIApplicationMain + 171
    21  Swipes and Shakes                   0x000000010ddd326d main + 109
    22  libdyld.dylib                       0x00000001107d092d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Solution

  • Declare swiped outside viewDidLoad and it should not crash

    class ViewController: UIViewController, UIGestureRecognizerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:")
        swipeRight.direction = UISwipeGestureRecognizerDirection.Right
        self.view.addGestureRecognizer(swipeRight)
    
        var swipeUp = UISwipeGestureRecognizer(target: self, action: "swiped:")
        swipeUp.direction = UISwipeGestureRecognizerDirection.Up
        self.view.addGestureRecognizer(swipeUp)
    }
    
    func swiped(gesture: UIGestureRecognizer) {
    
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
    
            switch swipeGesture.direction {
    
            case UISwipeGestureRecognizerDirection.Right:
                print("swiped right")
            case UISwipeGestureRecognizerDirection.Up:
                print("swiped up")
            default:
                break
    
            }
    
        }
    
    }