Exploring protocols
Protocols are like blueprints that determine what properties or methods an object should have. After you’ve declared a protocol, classes, structures, and enumerations can adopt this protocol and provide their own implementation for the required properties and methods.
Here’s what a protocol declaration looks like:
protocol ProtocolName {
var readWriteProperty1 {get set}
var readOnlyProperty2 {get}
func methodName1()
func methodName2()
}
Just like classes and structures, protocol names start with an uppercase letter. Properties are declared using the var
keyword. You use {get set}
if you want a property that can be read from or written to, and {get}
if you want a read-only property. Note that you just specify property and method names; the implementation is done within the adopting class, structure, or enumeration.
For more information on protocols, visit: https://docs.swift.org/swift-book/documentation/the-swift...