Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.2 #selector in protocol extension compiler error

I've got a protocol extension it used to work perfectly before swift 2.2.

Now I have a warning that tells me to use the new #selector, but if I add it

no method declared with Objective-C Selector.

I tried to reproduce the issue in this few lines of code, that can be easily copy and paste also into playground

  protocol Tappable {     func addTapGestureRecognizer()     func tapGestureDetected(gesture:UITapGestureRecognizer) }  extension Tappable where Self: UIView {     func addTapGestureRecognizer() {         let gesture = UITapGestureRecognizer(target: self, action:#selector(Tappable.tapGestureDetected(_:)))         addGestureRecognizer(gesture)     } }  class TapView: UIView, Tappable {     func tapGestureDetected(gesture:UITapGestureRecognizer) {         print("Tapped")     } } 

There is also a suggestion to append to that method in the protocol @objc, but if I do it asks me also to add it to the class that implements it, but once I add the class doesn't conform to the protocol anymore, because it doesn't seems to see the implementation in the protocol extension.
How can I implement this correctly?

like image 771
Andrea Avatar asked Mar 23 '16 17:03

Andrea


People also ask

What is the Swift latest version?

Swift 5.1 was officially released in September 2019. Swift 5.1 builds on the previous version of Swift 5 by extending the stable features of the language to compile-time with the introduction of module stability.

Is Swift a good programming language?

Swift is a powerful and intuitive programming language for iOS, iPadOS, macOS, tvOS, and watchOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design and produces software that runs lightning-fast.

What is Swift programming language used for?

Swift is a robust and intuitive programming language created by Apple for building apps for iOS, Mac, Apple TV, and Apple Watch. It's designed to give developers more freedom than ever. Swift is easy to use and open source, so anyone with an idea can create something incredible.

Can I code Swift on Linux?

Swift is such an amazing language, beautiful and fast, but you can't use it for server-side programming if you're a Linux user.


1 Answers

I had a similar problem. here is what I did.

  1. Marked the protocol as @objc.
  2. Marked any methods I extended with a default behavior as optional.
  3. Then used Self. in the #selector.

    @objc public protocol UpdatableUserInterfaceType {   optional func startUpdateUITimer()   optional var updateInterval: NSTimeInterval { get }   func updateUI(notif: NSTimer) }  public extension UpdatableUserInterfaceType where Self: ViewController {    var updateUITimer: NSTimer {     return NSTimer.scheduledTimerWithTimeInterval(updateInterval, target: self, selector: #selector(Self.updateUI(_:)), userInfo: nil, repeats: true)   }    func startUpdateUITimer() {     print(updateUITimer)   }    var updateInterval: NSTimeInterval {     return 60.0   } } 
like image 130
someoneAnyone Avatar answered Sep 20 '22 13:09

someoneAnyone