Recall & Review
beginner
What is a computed property in Swift?
A computed property calculates a value each time it is accessed instead of storing it. It uses a getter and optionally a setter to compute the value dynamically.
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, like this:<br>
var area: Double {<br> return width * height<br>}This property calculates and returns the area without storing it.Click to reveal answer
intermediate
Can computed properties have setters in Swift? What does that mean?
Yes, computed properties can have setters. This means you can assign a value to the property, and the setter will run code to update other properties or perform actions based on the new value.
Click to reveal answer
beginner
Why use computed properties instead of stored properties?
Computed properties save memory because they don’t store values. They always provide up-to-date values based on other data, making your code cleaner and more flexible.
Click to reveal answer
beginner
Example: How to add a computed property 'fullName' combining 'firstName' and 'lastName'?
You can write:<br>
var fullName: String {<br> return "\(firstName) \(lastName)"<br>}This property returns the full name by joining first and last names.Click to reveal answer
What does a computed property in Swift do?
✗ Incorrect
Computed properties calculate their value dynamically each time you access them.
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 setters to allow assigning values that update other data.
What happens if you omit the setter in a computed property?
✗ Incorrect
Without a setter, the computed property is read-only and only has a getter.
Why might you choose a computed property over a stored property?
✗ Incorrect
Computed properties calculate values on demand, so they don’t use extra memory and always reflect current data.
Explain what a computed property is and how it differs from a stored property in Swift.
Think about how the value is provided or stored.
You got /3 concepts.
Write a simple example of a computed property that returns the full name by combining first and last names.
Use string interpolation inside the getter.
You got /3 concepts.