Lazy and Computed Properties
Lazy and Computed Properties
Lazy and Computed Properties is a free Swift Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Introduction
Hello,
In the previous lesson, we learned about stored properties. You can also define lazy and computed properties.
What I saw in the previous section was to store the data (Store) of stored properties.
Apart from that, we can define properties as computed and lazy.
Lazy Properties
Lazy Properties
When a property value will not need to be generated until the first time the property value is used, you can use a lazy stored property.
When a property value is not used until its first use, we can use it as a lazy stored property. Therefore, if the production of an object is costly and it is not necessary for the first time, we will pass the production of the object at the first use of that object and avoid unnecessary resource consumption and performance loss. In order to make a stored property a lazy stored property, we need to pass the lazy modifier.
class Person {
lazy var name: String = "Name"
var age:Int = 0
private var _lastName:String = ""
func profile() -> String {
return "I'm \(self.name) and I'm \(self.age) years old."
}
}
var p = Person()
p.name = "Coddy"
p.age = 10
print(p.profile())
Computed Properties
Computed Properties
Computed properties receive an input and freeze the new result. In fact, he might think at first glance that it works like functions. However, computed properties are used with dot syntax just like properties.
In the example below, we can better understand the difference between the method and computed property.
cont.
In addition to stored properties, classes, structures, and enumerations can define computed properties, which do not actually store a value.
Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.
example
If you define get { } inside the property declaration, it makes that property to a computed property. And it cannot have initial value as when you access the property, it will always call get{} function declared in property.
class Person {
lazy var name: String = "Name"
var age:Int = 0
private var _lastName:String = ""
var lastName:String{
get {
return _lastName
}
set {
_lastName = newValue
}
}
var profile:String{
get{
return "I'm \(self.name) and I'm \(self.age) years old."
}
}
}
var p = Person()
p.name = "Matt"
p.age = 40
/*
Normally, if it was a method, we would call it with the p.profile () method.
However, since it is propery, it was enough to call it with .profile.
*/
p.profileCongratulations
Congratulations 🎉
In this lesson, we learned Lazy and Computed Properties on Swift.
See you in the next lesson.

Frequently asked questions
Is the “Lazy and Computed Properties” lesson free?
Yes — the full text of “Lazy and Computed Properties” is free to read here on the web, and the Swift Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Lazy and Computed Properties”?
Lazy and Computed Properties You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.
How long does the “Lazy and Computed Properties” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.