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.
extension MathGenius {
func calculateGPA() {
print("I'm too cool for skool")
}
}
You don’t have to provide the default method due to .
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
.
extension FindAreable {
let area = String(side * length)
return "The area is \(area)"
}
}
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.