Notes: 32. Challenge: Properties
Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.
It’s time for some practice with properties. Again, everything you need is in the playground for this part of the course. Pause the video, and write some code to complete the exercises. Then, keep watching to compare your code with my solutions. Time to pause the video; have at it!
First, I began with a struct named Temperature, and gave it two Double properties, as instructed, with names that looked a little bit like how degrees are normally displayed.
struct Temperature {
var degreesF: Double
var degreesC: Double
}
Then going with the first hint, I made my “degrees Celsius” property computed based on the conversion information in the second hint.
38 var degreesF: Double
var degreesC: Double {
get { return (degreesF - 32) / 1.8 }
}
And to make “degrees Fahrenheit” have an accurate value, when setting “degrees Celsius”, I reversed that math for the setter, using the “new value”,
42 set { degreesF = newValue * 1.8 + 32 }
I could have stored the Celsius value, and computed the Fahrenheit value instead. You could go either way, but one of them has to be stored. With my struct ready, I tested it out, at 32 degrees Fahrenheit, which is where water freezes.
46 var temperature = Temperature(degreesF: 32)
temperature.degreesC
Then, I handled the second challenge, starting with an if statement in the didSet observer for degreesF.
39 didSet {
if degreesF > 100 {
}
}
Inside, I printed a pertinent message.
41 print("It's \(degreesF) degrees Fahrenheit! I had fun coding in Swift with you before I melted.")
And because the Celsius and Fahrenheit values were in sync, I could set degreesC and still see my message, when appropriate.
temperature.degreesC😺 = 75
The last challenge revisits our month and season enumerations! The task was to turn both of the functions into computed properties for a Month. I did monthsUntilJingleBells first, and I started by copying and pasting the function into Month, below the cases.
func monthsUntilJingleBells(from month: Month) -> Int {
Month.december.rawValue - month.rawValue
}
To turn that into a computed property, I replaced func with var
😺var🛑 monthsUntilJingleBells(from month: Month) -> Int {
and then set the type to Int, getting rid of the parameter list and return token.
var monthsUntilJingleBells😺: 🛑Int {
Inside of the property, I just got rid of month. rawValue is accessible within the enumeration, as is.
Month.december.rawValue - ❌month.❌rawValue
The second function when similarly! I copied the function into the enumeration, changed func to var, and got rid of the parameter list.
😺var season: Season🛑 {
The only thing left to change, was what value to switch on. The directions reminded me that I can use self to refer to the current instance, so I switched on self.
switch 😺self🛑 {
Then I could test out both propeties!
Month.august.monthsUntilJingleBells
Month.august.season
August is in the summer, and it’s 4 months until Jingle Bells time.