Understanding property observers
Each superhero has a running speed score that determines how fast he will move when running; therefore, we will add a public runningSpeedScore
property. We will change the initializer code to set an initial value for the new property. However, this new property has some specific requirements.
Whenever the running speed score is about to change, it will be necessary to trigger a few actions. In addition, we have to trigger other actions after the value for this property changes. We might consider adding code to a setter method combined with a related property, run code before we set the new value to the related property, and then run code after we set the new value. However, Swift allows us to take advantage of property observers that make it easier to run the code before and after the running speed score changes.
We can define a public runningSpeedScore
property with both a willSet
and didSet
methods. After we create an instance of the new version of the SuperHero...