Skip to main content

Swift - Extensions

  • Swift extensions can add fields, functions, and initializers to all the named types!
    • Swift named types:
      • class
      • struct
      • enum
      • protocol

Protocol and Extensions

  • The extensions have even more power with protocols!
    • We can extend a named type so that it conforms to a protocol.
    • We can define a default method implementation for a protocol.

Protocol default methods implementations

Consider this:

classDiagram
Animal <|.. Dog
class Animal {
+name: String
+speak()
}
class Dog {
+name: String
+speak()
}

We can define a default implementation for speak() by defining an extension:

protocol Animal {
var name: String { get }
init(name: String)
func speak()
}

extension Animal {
func speak() {
print("I'm an animal!")
}
}