I am trying to add a UITapGestureRecognizer
to a imageview
as shown below
let gest=UITapGestureRecognizer(target: self, action: Selecto
You should use following syntax:
let gest = UITapGestureRecognizer(target: self, action: #selector(imgPressed))
Note, that Selector
can accept initial type Void
as well as String
. Properly old-style Selector
initialization looks like this:
let action: Selector = Selector("imgPressed")
Now you may check, that if you will try combine new style with old style you will get compilation error:
let action = Selector(imgPressed) // Error!
Funny thing is that you probably will try to resolve this error by appending bracers, and you will not have any errors after that! But this is completely useless. Look at following 3 lines of code, they are equivalent
let action = Selector(imgPressed())
let action = Selector(Void())
let action = Selector()
exept one thing - in first line you called imgPressed()
function, returned Void
, by your hands. Thats it =)