Computed Property in Swift

Object creation is a heavy process. When we create a class object, all the public and private properties of that class are initialised inside the constructor. Every variable inside a class initialisation requires a certain amount of time to allocate the memory on the heap and hold its reference on the stack. The more variables, the more time it may take but since the time is in microseconds or even less, it’s not observable.

This post was originally posted at https://agrawalsuneet.github.io/blogs/computed-property-in-swift/ and reposted on Medium on 03rd Dec 2021.

Sometimes we don’t need all the objects to be initialised during the class object creation itself.
There can be two reasons for that.

  1. That object/property/variable is dependent on another object to initialise first and use its reference.
  2. The flow is such that we need that object only in a certain condition.

In Swift, we have certain features which can help us in delaying that object initialisation as per the requirement.

One way of doing that is using computed properties.
Computed properties are the properties that don’t get initialised while object creation or constructor is called. They get computed every time the property is accessed.
We can use it for any heavy computation which we want to do on a conditional basis. Unlike Lazy properties, these properties get computed every time the property is accessed.

The basic initialisation of a computed property in a User class object by will look like below

struct User {
let name : String
let age : Int
}
struct Department {
var users : [User]

init(users : [User]) {
print("Department constructor is called")
self.users = users
}

var youngestUser : User? {
print("Department youngestUser is computed")
return self.users.min(by: {$0.age < $1.age})
}
}

Please continue reading at https://agrawalsuneet.github.io/blogs/computed-property-in-swift/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

iPhone back tap for the deaf-blind

UIDatePicker Date Change Listener : Swift

How to add amazing frame-by-frame animations in SwiftUI

How RTA Maryland Uses Rider Alerts to Update Passengers during COVID-19

Creating a custom rounded stepper in Swift

Dependencies Injection in a Modularized SwiftUI App With MVVM

12 Top iOS Libraries For Your App

Apple Iphone sitting on a Macbook computer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

What is a Lazy Stored Property and when to use it?

UIViewController Lifecycle

Swift : Understanding Mutating Functions in One Minute

Why You Should Adopt Swift Async/Await Right Now