Protocols
- Protocols define a set of required properties and methods that a type must implement.
- Protocols act as contracts, ensuring that conforming types adhere to specific functionality.
- Protocols don't provide implementations; they serve as blueprints for defining requirements.
- Types can conform to protocols by implementing all required properties and methods.
- Protocol conformance allows treating instances of different types as a shared type.
- Protocols can include properties that specify read-only or read-write requirements.
- Protocol arrays enable working with collections of objects conforming to the same protocol.
- Functions can accept protocols as parameters and return protocols as return types.
- A type can conform to multiple protocols by listing them separated by commas
:
.
protocol Vehicle {
var name: String { get }
var currentPassengers: Int { get set }
func estimateTime(for distance: Int) -> Int
func travel(distance: Int)
}
struct Car: Vehicle {
let name = "Car"
var currentPassengers = 1
func estimateTime(for distance: Int) -> Int {
distance / 50
}
func travel(distance: Int) {
print("I'm driving \\(distance)km.")
}
func openSunroof() {
print("It's a nice day!")
}
}
struct Bicycle: Vehicle {
let name = "Bicycle"
var currentPassengers = 1
func estimateTime(for distance: Int) -> Int {
distance / 10
}
func travel(distance: Int) {
print("I'm cycling \\(distance)km.")
}
}
func commute(distance: Int, using vehicle: Vehicle) {
if vehicle.estimateTime(for: distance) > 100 {
print("That's too slow! I'll try a different vehicle.")
} else {
vehicle.travel(distance: distance)
}
}
let car = Car()
commute(distance: 100, using: car) // I'm driving 100km.
let bike = Bicycle()
commute(distance: 50, using: bike) // I'm cycling 50km.
func getTravelEstimates(using vehicles: [Vehicle], distance: Int) {
for vehicle in vehicles {
let estimate = vehicle.estimateTime(for: distance)
print("\\(vehicle.name): \\(estimate) hours to travel \\(distance)km")
}
}
getTravelEstimates(using: [car, bike], distance: 150)
// Car: 3 hours to travel 150km
// Bicycle: 15 hours to travel 150km
Sample code
- Defines a protocol
BasketballPlayer
with dribble()
, shoot()
, and playerNumber
requirements.
- Implements the protocol in a
NBAPlayer
struct by providing the required methods and properties.
- Creates an instance of
NBAPlayer
, sets the playerNumber, and dribble or shoot.
protocol BasketballPlayer {
var playerNumber: Int { get }
func dribble()
func shoot()
}
struct NBAPlayer: BasketballPlayer {
let playerNumber: Int
func dribble() {
print("Player #\\(playerNumber) is dribbling the basketball.")
}
func shoot() {
print("Player #\\(playerNumber) is shooting the basketball.")
}
}
var player = NBAPlayer(playerNumber: 23)
player.dribble() // Player 23 is dribbling the basketball.
player.shoot() // Player 23 is shooting the basketball.