Nicholas Muir Nicholas Muir - 3 months ago 14
Swift Question

Cannot convert value of type 'NSObject -> () -> ViewController' to expected argument type 'AnyObject?'

I am trying to declare a gesture recogniser. When I declare it in the function it works fine but when I declare at the class level so it is available to multiple functions I get the error.


Cannot convert value of type 'NSObject -> () -> ViewController' to expected argument type 'AnyObject?'


I am declaring it like this:

let gestureBack = UIPanGestureRecognizer(target: self, action: Selector("wasDraggedBack:"))

Answer

If you want Gesture object available to multiple function than declare its instance at class level and initialize the object in viewDidLoad like this.

var gestureBack: UIPanGestureRecognizer = UIPanGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()
    self.gestureBack = UIPanGestureRecognizer(target: self, action: Selector("wasDraggedBack:"))
}