Jason Woodland Jason Woodland - 7 months ago 578
Swift Question

Swift wants argument of #selector to be exposed to Objective-C

I have a Swift project where I want to attach a method to a UIButton's tap event. I have the following code:

class MyClass {
let myButton = UIButton(frame: CGRectMake(50, 50, 100, 50))
init() {
myButton.addTarget(self, #selector(self.didTap(_:)), forControlEvents: .TouchUpInside)
}

func didTap(sender: UIButton) {
print("Tapped")
}
}


XCode highlights my
addTarget
line and says:

Argument of '#selector' refers to a method that is not exposed to Objective-C


If I add the
@objc
prefix to my
func didTap
like it suggests then everything works fine.

Do I have something enabled in my build settings which is causing this strange behaviour?

PS. I get this behaviour in 7.3.1. But if I try this in 7.2.1 it doesn't accept the
#selector(method(_:))
syntax, and
Selector("method:")
works fine.

Answer

Selectors are a feature of Objective-C and can only be used with methods that are exposed to the dynamic Obj-C runtime. You can't have a selector to a pure Swift method.

If your class inherits from NSObject then its public methods are exposed to Obj-C automatically. Since your class does not inherit from NSObject you have to use the @objc attribute to indicate that you want this method exposed to Obj-C so that it may be called with an Obj-C selector.

#selector() is the new syntax in Swift 2.2. It allows the compiler to check that the selector you're trying to use actually exists. The old syntax is deprecated and will be removed in Swift 3.0.