## Computed property question

 Feb '22 Hi, For computed properties, we are given this example: ``````struct Employee { let name: String var vacationAllocated = 14 var vacationTaken = 0 var vacationRemaining: Int { vacationAllocated - vacationTaken } }`````` Then we are advised if we want to "write to" the code, we should use a getter and a setter like this; ``````struct Employee { let name: String var vacationAllocated = 14 var vacationTaken = 0 var vacationRemaining: Int { get { vacationAllocated - vacationTaken } set { vacationAllocated = vacationTaken + newValue } } }`````` First, I don't understand what "write to" the code means. Secondly, I'm not sure why we need to use a getter and a setter. It seems like we can just change the initial variables in the first code to get the same information. Will appreciate it if someone will help clarify. Thank you, Arthur 2 Feb '22 Let me use a different example. ``````struct Programmer { let name: String var distanceFromWall = 30 var screenDistanceFromWall = 20 var distanceFromScreen: Int { distanceFromWall - screenDistanceFromWall } }`````` You might create a programmer object and see that, by default, the programmer is a distance of 10 from her computer's screen. If you change her distance from the wall to 40, she'll be a distance of 20 from her screen. You compute this value each time you want to know the distance. But WHAT IF you wanted to specify the distance between the programmer and her screen? One way would be to move the monitor, or reseat the programmer. However, with a setter, you can give the desired value to the computed property. Then you let the computed property either (1) move the programmer or (2) move the monitor. That's your business to decide! In this case you are "writing to" the computed property. ``````struct Programmer { let name: String var distanceFromWall = 30 var screenDistanceFromWall = 20 var distanceFromScreen: Int { get { distanceFromWall - screenDistanceFromWall // no change here } set { // easier to move the programmer, than move the screen! // Here you change the programmer's distance to the wall // so that the programmer is now "newValue" distance from the monitor. distanceFromWall = screenDistanceFromWall + newValue // moving the programmer! } } }`````` You are correct however. In the inital example, you can just change the vacation variables. But this is not the concept that @twostraws was teaching in this lesson. 3 Feb '22 Setting a computed property can also be useful when you want to keep the stored properties as `private` but still want to give the user the means to change them. ``````struct Account { let id = UUID() private(set) var balance: Double private var transactions: [Double] = [] init(initialBalance: Double) { balance = initialBalance } var latestTransaction: Double { get { transactions.last ?? 0 } set { transactions.append(newValue) } } } var acct = Account(initialBalance: 10_000_000) acct.latestTransaction = -10_000`````` In this case, we don't want the user to have direct access to the `transactions` array (because we don't want them doing `account.transactions[2]` or whatever), so we use a computed property to control access to the `private` property. 3 Feb '22 Thank you @Obelix & @roosterboy. This clarifies things a lot for me. 2

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Sponsor Hacking with Swift and reach the world's largest Swift community!

### Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

You are not logged in