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?
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.
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.
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.
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.
I had a similar problem. here is what I did.
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With