Programming in Swift · Challenge: Properties | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5994-programming-in-swift/lessons/42

Hello, when I run the code - it doesn’t matter what I set degreesF to in the get conversion, I never get the warning message. The only way I can get the warning is when I - tempConvert.degreesF = 101. Also - can you help me understand what the set does in the challenge? I comment it out and nothing changes.

Thank you!

Hi!

I can’t tell what you mean by “run the code” or “what I set degreesF to in the get conversion”. Could you perhaps paste your code here and elaborate on it?

It keeps degreesF in sync with what you set for degreesC.

For example, without it, the last line of this code would return 100, not 32 as it should.

var temperature = Temperature(degreesF: 100)
temperature.degreesC = 0
temperature.degreesF

I looked at it again and it makes sense - I was making it harder than it was! Thank you for responding! That’s what I get for coding late at night after a full day of work!

@jessycatterwaul please explain the sence why can’t we use willSet/didSet together with getter and setter?

Hi @andreysvx

Property observers (willSet / didSet) are only used on stored properties. The value for properties like this is stored, and the property observers let you react to any changes of that value.

Getters and setters are used for computed properties. Computed properties don’t have a stored value! Their value depends on some other properties or state, and is recomputed every time you access them.

Most of the material on this subject is the same, but it may help you to watch the updated version of this course. You can find the part on properties and methods here: https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/28

There are also some Stack Overflow discussions you may find helpful:

1 Like

thank you very much! It was very clarifying explanation )

Is that fair for structures only or for classes as well?

1 Like

Yes! That applies to structures and classes :]