Protocol extension for a specific conforming class

suggest change

You can write the default protocol implementation for a specific class.

protocol MyProtocol {
    func doSomething()
}

extension MyProtocol where Self: UIViewController {
    func doSomething() {
        print("UIViewController default protocol implementation")
    }
}

class MyViewController: UIViewController, MyProtocol { }

let vc = MyViewController()
vc.doSomething() // Prints "UIViewController default protocol implementation"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents