Challenge Set B:
At the beginning of the chapter, you saw a Car structure. You’ll dive into the inner workings of that car now. Rewrite the FuelTank structure below with property observer functionality:
struct FuelTank {
var level: Double // decimal percentage between 0 and 1
}
- Add a lowFuel stored property of Boolean type to the structure.
- Flip the lowFuel Boolean when the level drops below 10%.
- Ensure that when the tank fills back up, the lowFuel warning will turn off.
- Set the level to a minimum of 0 or a maximum of 1 if it gets set above or below the expected values.
In the previous chapter, you learned about properties, which are constants and variables that are part of structures. Methods, as you’ve already seen, are merely functions that reside inside a structure.
In this chapter, you’ll take a closer look at methods and initializers. As with properties, you’ll begin to design more complex structures. The things you learn in this chapter will apply to methods across all named types including classes and enumerations which you’ll see in later chapters.