Computed Property in Swift

This post was originally posted at https://agrawalsuneet.github.io/blogs/computed-property-in-swift/ and reposted on Medium on 03rd Dec 2021.
  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.
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/

--

--

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