Introduction to Protocol

Introduction

Welcome to Lesson 1 of Protocol Oriented Swift. In this lesson, the goal is to master the protocol syntax before you take full advantage of what it offers on the table.

Problem

Problem: Protocols, who are you?

Analogy: Protocol is like a basketball coach. He/she tells players what to do, but he/she doesn’t know how to dunk.

Drawbacks of OOP

Before implementing protocols, you should be aware of drawbacks from solely relying on OOP.

  1. When you subclass, you have to inherit properties and methods which you may not need. The object becomes unnecessarily bloated.
  2. When you make a lot of super classes, it becomes extremely hard to navigate between each class and fix bugs/edit.
  3. Since objects are referencing to the same place in memory, if you make a copy and create a small change its property, it can mess up the rest. (Mutability due to reference)

Design Protocol

Create a protocol called, Humanable. It contains two properties and one method. Do not worry about get and set for now.

  1. protocol Humanable {
  2. var name: String { get set }
  3. var race: String { get }
  4. func sayHi()
  5. }

Create a class that conforms to Korea just like how you would subclass. To prevent Swift from screaming, you must implement two properties and one method defined in the protocol.

  1. struct Korean: Humanable {
  2. var name: String = "Bob Lee"
  3. var race: String = "Asian"
  4. func sayHi() {
  5. print("Hi, I'm \(name)")
  6. }
  7. }
  8. class American: Humanable {
  9. var name: String = "Joe Smith"
  10. var race: String = "White"
  11. func sayHi() {
  12. print("Hi, I'm \(name)")
  13. }
  14. }

Protocol Inheritance

A protocol may “inherit” the requirements from other protocols.

  1. protocol SuperHumanable: Humanable {
  2. var canFly: Bool { get set }
  3. func punch()
  4. }

Any classes, structs, enums that conform the SuperHumanable must contain properties and methods from Humanable as well.

  1. struct BobtheDeveloper: SuperHumanable {
  2. var name: String = "Bob"
  3. var race: String = "Asian"
  4. func sayHi() {
  5. print("I code")
  6. }
  7. var canFly: Bool = false
  8. func punch() {
  9. print("I don't punch")
  10. }
  11. }

Important: { get } or { get set } only apply to computed property. If a class has a stored property, you may choose either { get } or { get set }.

Computed Property

When a class/struct/enum contains computed properties, you must take into account of { get } or { get set } while setting the requirements of a protocol.

Create a protocol called, Breathable. It contains two properties: isBreathing and isLiving.

  1. protocol Breathable {
  2. var isBreating: Bool { get set }
  3. var isLiving: Bool { get }
  4. }

Create a struct that conforms to Breathable. The struct contains properties: isBreathing and isLiving. Define them as computed properties.

  1. struct Human: Breathable {
  2. var isBreating: Bool {
  3. get { return true }
  4. set {}
  5. }
  6. var isLiving: Bool {
  7. get { return true }
  8. set {}
  9. }
  10. }

Rule: When you set the requirement as { get } you may make the property gettable or settable. If it is defined as { get set }, you must define the property as settable.

{ get } { get set }
Stored Property ? Stored Property ?
Computed Property —> Gettable, Settable (Optional) Computed Property —> Gettable, Settable (Must)

Source Code

4001_intro_protocols.playground

References

All evidence points to OOP being bullshit by John Barker

If you’re subclassing, you are doing it wrong by Hector Matos

Conclusion

For stored properties, it does not matter whether you use { get } or { get set }. For computed properties, however, if you define the requirement as { get set }, the property must be settable. If it is { get } alone, the property can be gettable or settable. Remember, protocol is like a basketball coach. structs, enums, classes, are like players.

In the following lesson, you will learn how to create a scalable architecture with protocol extension, a.k.a, the magic.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.