Protocol Extension

    I don’t even want to type anymore.

    Create a protocol called that contains one method.

    Create Protocol Extension

    Create an extension to MathGenius. The extension contains a default method.

    1. extension MathGenius {
    2. func calculateGPA() {
    3. print("I'm too cool for skool")
    4. }
    5. }

    You don’t have to provide the default method due to .

    1. struct Bobby: MathGenius {}

    Create a protocol that contains a method that takes two Double parameters and returns String.

    Create an extension to FindAreable which will return a statement whose type is in String.

    1. extension FindAreable {
    2. let area = String(side * length)
    3. return "The area is \(area)"
    4. }
    5. }

    Usage Case

    • UILabel, UIImageView, UIView —> Animation
    • Reusable table and collection view cells

    If you are interested in how you may apply to UIKit, you may read Protocol Oriented View with Bob (Blog).

    Source Code

    4002_protocol_extension.playground

    Protocol Oriented Swift is only limited by your imagination.