I've a base class that implements an extension that conforms to a protocol as below:
protocol OptionsDelegate {
func handleSortAndFilter(opt: Options)
}
extension BaseViewController: OptionsDelegate {
func handleSortAndFilter(opt: Options) {
print("Base class implementation")
}
}
I've a subclass "InspirationsViewController" that inherits from BaseViewController. And I'm overriding protocol method in the extension as below:
extension InspirationsViewController {
override func handleSortAndFilter(opt: Options) {
print("Inside inspirations")
}
}
I'm getting error when I override "handleSortAndFilter" function in subclass extension: "Declerations in extensions cannot override yet"
But I'm not seeing similar problem when I implemented UITableView datasource and delegate methods.
How to avoid this error?
Use protocol extension with where clause. It works. But I would not recommend you to have such things in your codebase.
class BaseViewController: UIViewController {
}
extension OptionsDelegate where Self: BaseViewController {
func handleSortAndFilter(opt: Options) {
print("Base class implementation")
}
}
extension BaseViewController: OptionsDelegate {
}
class InsipartionsViewController: BaseViewController {
}
extension OptionsDelegate where Self: InsipartionsViewController {
func handleSortAndFilter(opt: Options) {
print("Inspirations class implementation")
}
}
As far as I know you can't override methods in extensions. Extensions can only do the following: “Extensions in Swift can:
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 3.0.1).”
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