Introduction to Protocols in Swift 3

Jayven N
iOS App Development
4 min readFeb 28, 2017

Once this makes sense to you. It’s beautiful.

Hello. Explain yourself.

Hey Apple, what is a protocol?

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.

— Apple

To me it reads:

Read the definition again and call it a day.

— Apple

Hence, I am writing this article to clarify things up. Make things interesting at the same time too. We will use a UFC fighter example for our protocol later and hopefully it will all make sense at the end.

WHAT IS A PROTOCOL

A protocol is a list of prerequisites to inherit a title. That’s the formal definition.

If there’s one thing to help you remember what a protocol is, this is the gangster definition:

A protocol is you need this, this, and this. To be called this.

You need to drink, party, and procrastinate to be called a stereotypical college student. This is how I remember and explain to people what a protocol is.

PROTOCOL SYNTAX

Image from Apple

Syntax structure similar to a class/struct/enum.

UFC FIGHTER PROTOCOL

One day. Wake up. You just want to be a UFC fighter. You went up to Dana White (UFC president) and you ask how do I become a UFC fighter?

“No problem. Let’s talk prerequisites, properties and methods.” — Dana White

Let’s begin with properties. Things you need to have to be a UFC fighter. You need to have a definite name and an optional nickname.

What this means? This means you can’t change your name after it’s set. However, you can change your nickname after it’s set. Nickname can also be nil.

Now let’s talk about methods. Things you need to be able to do to be a UFC fighter. You definitely need to be able to punch, kick, and grapple in the UFC. Aka knowing the fundamentals. You optionally need to be able to trash talk.

What this means? This means trash talking can be an ability of a UFC fighter. However, you don’t have to have it. You can just straight up fight.

I have a feeling you are already feeling the UFC protocol in you. If not, you will feel it a lot more by the end.

We have defined and explained our specs. Now let’s see how we can define the protocol.

STEPS TO DEFINE A PROTOCOL

1. Structure and name the protocol

protocol UFCFighter {}

2. Add property requirements

protocol UFCFighter {    var name: String { get }
var nickname: String? { get set }
}

So get and set. Let’s clarify. These are called variable properties. Alone you will call get, gettable. Call set, settable. They are as they sound.

So to communicate var nickname we can say:

We declared a nickname variable of type optional string with gettable and settable properties.

3. Add definite method requirements

protocol UFCFighter {    var name: String { get }
var nickname: String? { get set }
func punch()
func kick()
func grapple()
}

We added punch, kick, and grapple as must haves.

4. Add optional method requirements

@objc protocol UFCFighter {    var name: String { get }
var nickname: String? { get set }
func punch()
func kick()
func grapple()
@objc optional func trashTalk()}

We just added our optional trash talk method.

See if you spot the surprise present. Scary looking aye? Don’t worry. This is just the syntax we use to declare a protocol with an optional method. First, we add @objc keyword in front of the protocol keyword. Then we add the @objc optional keyword in front of our optional method.

WHY PROTOCOLS

Protocols are great because it makes sure that you have everything you need. It doesn’t allow you to forget anything. It’s like if you go to school and you have a protocol with you, you have to remember to bring your laptop and whatever school equipment you need with you.

WHERE ARE PROTOCOLS

You probably have seen a bunch of protocols before like table view delegate and data source. Those are an example of a protocol.

The way that we normally use them is called a class adoption. We have a our UIViewController and we adopt the protocols. I am just going to name the most common use cases just so we are aware of what’s out there. Protocols use cases include class/struct/enum adoptions, delegation patterns, and protocol oriented programming.

LAST REMARKS

I hope you have enjoyed and learned something valuable from my article. If you have then let me know by hitting that ❤ button and follow me on Medium. Also, share this article so that your circle can make some knowledge gains too.

For those interested, here is my LinkedIn.

Lastly if you have any comment, question, or recommendation, feel free to drop them below.

--

--