Protocol extensions
suggest changeProtocol 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents