Understand Protocols
Tags: swift, ios, protocols, protocol
Published: 2021-10-06 06:09:52

When I studied Swift, It was big challenge to understand about Protocols. So I tried to summery it easily for future Daniel. 🤗

##Concept of Protocols
Protocol is __*set of requirements*__ without detail contents as abstract.
For example,
Here is a high school with many teachers for various class.
A requirements from principle of the school is_“Education”_to teachers with curriculum structure. The structure doesn’t include detail contents. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrw4avoqibx4kvnzcmpu.png)

Each teacher can implement own curriculum based on the structure for students.
Then mathematic teacher implements curriculum for _mathematic_ class.
And chemical teacher implements curriculum for _chemical_ class.
So each teacher can implement the requirement what he/she wants based on the _curriculum structure_.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rcd3z8s1whgbtvdl3sh.png)



##Declare of Protocols

###Protocol can be defined as below 
1. Protocol keyword
2. Protocol name
3. Requirements
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmaogagj7ez824goodov.png)

For example by concept,
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n43pwnba1919slfp6goa.png)

##Adopt Protocols
Let’s learn how can __*adopt*__ protocols to class or structure.
Structures and classes can adopt protocols, you add a set of __*colon*__ and your add your __*protocol name*__ after the name of your structure and class.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbpbsou8xv28otvoa1tq.png)

And the __*multiple protocols*__ can be adopted with comma to classes and structures.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dh0b2i7xcu7ing8rtrqy.png)

##Confirm Protocols
After adoption of protocols, The requirements of the protocols should be implemented in classes or structures what adopted the protocols. We call it __*“Confirm the protocols”*__

Les’s use“Education”example again to learn confirming.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbfq0mya6pe2ivyct0yc.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tuvku5mjlsx4vrd5dxn3.png)


#Coding practice
Let’s practice protocols by coding.
1. Run Xcode to make a project
2. Choose macOS template
3. And choose “Command Line Tool”
4. Put Product Name as “Protocols” and save project
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omvda2k7krggoxp6lk9t.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54v9wqcw8hrxbqx9gfhx.png)

##Tips
1) To adopt protocols, always the protocols should be adopted
__after super class__(or other inherit of class)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56bp3e5ff0pv6horbmsw.png)

2) As you know, Classes has super class and child class for inheriting but Structures doesn’t have super structure and child structure. BUT !!! Protocols can be adopted to classes and structures for both.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e3rtlykxd4bor4tyh82.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/osyapuf260xfnfcstoks.png)

Thank you for your reading.
Daniel Jang