3

In my viewDidLoad function, I setup a swipe gesture recognizer:

var swipeRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("move"))
swipeRecognizer.direction = .Right
view.addGestureRecognizer(swipeRecognizer)

And then I set up the move function:

func move(swipe:UISwipeGestureRecognizer) {
    NSLog("swiped")
}

However, I keep getting the following error when I swipe right:

[_TtC8swiftris9GameScene move]: unrecognized selector sent to instance 0xc81c200
2014-06-03 14:52:57.560 swiftris[45440:6777826] 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[_TtC8swiftris9GameScene move]: unrecognized selector sent to instance 0xc81c200'

What could be the problem?

Alexis King
  • 43,109
  • 15
  • 131
  • 205
codevinsky
  • 1,273
  • 1
  • 9
  • 18

2 Answers2

4

You defined your function as move(swipe:UISwipeGestureRecognizer), which maps to the obj-c method name move:, but your selector is just "move". You need to use "move:" instead.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
3

As @Kevin Ballard correctly points out, your selector doesn't match your method, which explains the "unrecognized selector" exception. However, I think it's worth noting that you can ditch the cast to Selector altogether, and use a string literal in its stead.

Quote from

You can construct a selector with a string literal, such as let mySelector: Selector = "tappedButton:". Because string literals can be automatically converted to selectors, you can pass a string literal to any method that accepts a selector.

Example:

let gesture: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action:"move:")
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281