Protocol Extensions in Swift

You can extend protocols too!

Protocol extensions are extremely powerful in Swift, but contain a couple of inherent limitations. To learn what they are, read on!

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • Use of extensions in Swift (guide HERE)

Terminology

class: An object that defines properties and methods in common

Conform: Conforming to a protocol means that anything that conforms to the protocol contains the necessary behaviours specified

Extensions: Extensions add new functionality to a class, struct, enum or protocol

protocol: A blueprint on methods, properties and requirements to suit a piece of functionality

struct: An object defined in Swift, using pass by value semantics

Why extend a protocol?

A protocol allows objects, and provide a concrete implementation of any object that conforms to it. This allows addition of functionality into any types that conform to the protocol, and even better we can extend the functionality without changing the original protocol.

Basic protocol extension syntax

Here is an example of a struct conforming to a protocol:

protocol Moves {
func walks()
}
struct Person {
var name: String
func walks() {
print ("Will walk")
}
}
let anne = Person(name: "Anne")

The person above can then be extended by adding a protocol extension. The magic of this is that we don’t need to alter the original protocol as we are providing a default implementation for that protocol.

What does this look like?

extension Moves {
func test() {
print ("this is a test")
}
}

Using the default implementation

Other changes? None. Our person uses the default implementation given to us in the protocol extension. We can even call this test on our person that we have implemented above

anne.test() // this is a test

If we don’t want to use the default implementation we can give any object that conforms to Moves an implementation.

Avoiding the default implementation:

The whole playground can be seen here:

Default parameters

In Swift we might be aware that Protocols have an interesting relationship with default parameters.

Because both structs and classes can conform to protocols, this section will have a different example showing a class conforming to a different protocol.

This appears to be a great little calculator (which ok, is overstating it a little bit).

However, we might like this addition function to return just the original number if one is provided. Usually this would take place with a default parameter.

This seems to be a error — you can’t use a default argument in a protocol.

Image for post
Image for post

Luckily we already have the answer, this is solvable through Extensions. We still conform to the protocol if we have the default implementation:

Conclusion…

Swift gives us ways around many small annoying problems. One of the potential problems is the use of default parameters in protocols.

How can this be solved? Protocol extensions with default implementations.

Problems (sic) solved.

Want to get in contact? Try the link here:

https://twitter.com/stevenpcurtis

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store