Protocol extensions

suggest change

Protocol extensions arrived in Swift 2. They provide a default implementation for every type that conforms to the protocol, without a shared base class.

protocol FooProtocol {
    func doSomething()
}

extension FooProtocol {
    func doSomething() {
        print("Hi")
    }
}

class Foo: FooProtocol {
    func myMethod() {
        doSomething() // By just implementing the protocol this method is available
    }
}

This is also possible using generics.

Feedback about page:

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



Table Of Contents