Implementing properties
Recall that properties in protocols come with a get and possibly a set requirement and that a conforming type must conform to at least these requirements.
Upgrade Bike to a WheeledVehicle:
class Bike: WheeledVehicle {
let numberOfWheels = 2 var wheelSize = 16.0
var peddling = false
var brakesApplied = false
func accelerate() { peddling = true brakesApplied = false
}
func stop() { peddling = false
brakesApplied = true
}
}
The numberOfWheels constant fulfills the get requirement. The wheelSize variable fulfills both get and set requirements.
Protocols don’t care how you implement their requirements, as long as you implement them. Your choices for implementing a get requirement are:
- A constant stored property
- A variable stored property
- A read-only computed property
- A read-write computed property
Your choices for implementing both a get and a set property are limited to a variable stored property or a read-write computed property.