This is a beginner blog on Protocols using the Swift programming language. Swift is a protocol-based language which favours value types over reference types and it is a big jump from the legacy Objective-C codebases you’ll see on a day-to-day basis. However, when I was learning about protocols, I just really struggled to conceptualise what their value was. So I came up with a story in my head about a work night out: the most relaxing environment for a team of programmers, right?….

Social Situation Warning

‘What is a Protocol ?’

Picture this: it’s a work night out, all the programmers have had a few drinks and everyone’s walls are down a little. People are actually utilising the archaic vibrating vocal folds located inside their larynx to communicate and not clicking away at a chat service with their unnaturally-fast typing skills despite being located mere metres away. But come one… it’s still super awkward right? How better to make it less scary than to chat about some protocols.

A protocol is something we follow on a day-to-day basis and in lots of different social situations. This situation involves the ‘Small Talking protocol’. It describes the absolute basic requirements for what you’re expected to do in this super-busy situation… ah the joys of work nights out…

protocol SmallTalking {
func makePoliteChitChat()

As you can see this protocol only has one thing you have to do when using it: make polite chit chat! Notice that everyone expects you to do this! You could be terrible at it, you might be a master at it and some will be better than others at talking about nothing but seemingly talking about something: what an art from small talk really is. However, what matters is that despite the fact that how everyone does it may be implemented differently, what is vital is that everyone says something. If you don’t implement that method, you sit there in absolute silence and no one can communicate with you. So sad 🙁

struct Merrida: SmallTalking {
func makePoliteChitChat() {
   say("Hey there. My name is Claire. What are your hobbies?")

Here Merrida has implemented an astounding example of evolutionary dialogue.

struct Hamish: SmallTalking {
func makePoliteChitChat() {
say("Alright? Busy tonight isn't it?")

Hamish has spent weeks practicing his opening line as well.

As you can see, Merrida and Hamish are destined for greater things and their chit chat sounds so, so natural. However what is clear is that they both conform to the SmallTalking protocol which means they definitely, definitely and magnificently implement the makePoliteChitChat method. How they implement it may vary, but they do implement it, even in their own special way.

A protocol is simply a way to describe how things can communicate and interact with other things and their design and requirements for how they should operate in those situations. You’ll find them everywhere and the more you use swift, the more you’ll grow to love them.

Leave a Reply

Your email address will not be published. Required fields are marked *