Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

ios - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...