Recall & Review
beginner
What is a computed property in Swift?
A computed property does not store a value directly. Instead, it provides a getter and optionally a setter to calculate a value when accessed or set.
Click to reveal answer
beginner
How do you define a read-only computed property in Swift?
You define a computed property with only a getter and no setter. For example:<br>
var area: Double {<br> return width * height<br>}Click to reveal answer
intermediate
Can computed properties have side effects in Swift?
Yes, computed properties can run code inside their getter or setter, so they can have side effects like logging or updating other values.
Click to reveal answer
beginner
What is the difference between stored and computed properties?
Stored properties hold actual values in memory. Computed properties calculate values on demand without storing them.
Click to reveal answer
intermediate
Show an example of a computed property with both getter and setter in Swift.
Example:<br>
var temperatureCelsius: Double {<br> get { return (temperatureFahrenheit - 32) * 5 / 9 }<br> set { temperatureFahrenheit = newValue * 9 / 5 + 32 }<br>}Click to reveal answer
What does a computed property in Swift do?
✗ Incorrect
Computed properties calculate and return a value when accessed, instead of storing it.
Which keyword is used to define a computed property in Swift?
✗ Incorrect
Computed properties are defined using 'var' because their value can change.
Can a computed property have a setter in Swift?
✗ Incorrect
Computed properties can have a setter to update related values.
What happens if a computed property has only a getter?
✗ Incorrect
A computed property with only a getter is read-only.
Which of these is NOT true about computed properties?
✗ Incorrect
Computed properties do not store values directly; they calculate them.
Explain what a computed property is and how it differs from a stored property in Swift.
Think about how values are stored or calculated.
You got /3 concepts.
Write a simple Swift computed property that calculates the area of a rectangle given width and height stored properties.
Use a getter to calculate the area.
You got /4 concepts.