Swift overriding protocol extension keeping the extension behaviour

RodolfoAntonici

I have this simple class and an hypothetical protocol called 'ShowAlert', it's extension to do the default implementation and a default ViewController and it's ShowAlert protocol implementation.

protocol ShowAlert {
    var titleForAlert: String! { get }
    func messageForAlert() -> String!
    func show()
}

extension ShowAlert where Self: UIViewController {

    func show(){
        let alert = UIAlertController(title: self.titleForAlert, message: self.messageForAlert(), preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .Cancel, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }


    @IBAction func showItNow(sender: AnyObject) {
        self.show()
    }



}

extension ViewController: ShowAlert {
    var titleForAlert: String! {
        get{
            return "Foo"
        }
    }


    func messageForAlert() -> String! {
        return "Bar"
    }

    func show() {
    // here I want to call the default implementation of the protocol to show the alert, then do something else
        print("Good day sir!")
    }
}

It's like on a subclass where I could call a 'super.show()' and then continue implementing whatever I want to do after that.

There's any way to do it? Or my logic go against what protocols are design for and that don't suppose to happen?

Qbyte

There is a simple solution: Just add a defaultShow method to the extension.

extension ShowAlert where Self: UIViewController {

    func defaultShow(){
        let alert = UIAlertController(title: self.titleForAlert, message: self.messageForAlert(), preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .Cancel, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }

    func show() {
        defaultShow()
    }
}

So in your code you can just call defaultShow:

extension ViewController: ShowAlert {
    // ...

    func show() {
        self.defaultShow()
        print("Good day sir!")
    }
}

There is also another solution where you can call .show() instead of .defaultShow(). However it uses casting and breaks encapsulation. If you want to see it let me know.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift overriding extension method

Swift : protocol extension and arrays

Dispatching to Swift protocol extension

Swift extension that conforms to protocol

Swift property observer in protocol extension?

Swift 2: UITableViewDataSource protocol extension

Swift: Is it possible to add a protocol extension to a protocol?

Swift protocol extension with property conforming to protocol

Swift: Providing a default protocol implementation in a protocol extension

Swift 3 protocol extension using selector error

Swift 2.2 #selector in protocol extension compiler error

How does protocol extension work in Swift?

Swift protocol extension self reference issues with init

Swift protocol extension with specific Self type

Conforming a generic type to a protocol in a Swift extension

Rust equivalent to Swift's extension methods to a protocol?

How to override computed property of protocol extension in Swift

Invalid Redeclaration of Variable in Swift Protocol Extension

Swift protocol conformance by extension between frameworks

Swift Class Extension Only When Conforming to Protocol

swift 2.0 - UITextFieldDelegate protocol extension not working

Protocol extension method dispatch in Swift 2.0

Swift extension only when conforming to Class AND protocol

Swift protocol extension method dispatch with superclass and subclass

Issue with the call hierarchy of a swift protocol extension

Default implementation of protocol extension in Swift not working

Overriding onInterceptTouchEvent in extension method

Protocol Extension in Swift Where Object Is a Class and conforms to a Protocol

Swift: Store Type of implementation of Protocol in static var of protocol extension