UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Question regarding "How to compute property values dynamically"

Forums > 100 Days of SwiftUI

Hello! I have a question about the video titled "How to compute property values dynamically." I got a bit distracted while watching and trying to understand what Paul was teaching.

Around the two-minute mark of the video, there was this code:

struct Employee {
    let name: String
    var vacationAllocated = 14
    var vacationTaken = 14

    var vacationRemaining: Int {
        vacationAllocated - vacationTaken
    }
}

I'd like to clarify whether var vacationRemaining is a closure. Thank you!

The reason it might seem like a closure to me is because, even though it's a function, it doesn't have () like regular functions. My understanding of closures is that they are quite similar to functions, but they can be created more spontaneously, almost 'on the fly,'. Furthermore, we've initialized it using a var keyword which is similar to how closures are created although closures have = signs in between them. However, I'm not entirely sure if my understanding is accurate, so I'd greatly appreciate any corrections or clarifications.

EDIT: My apologies, around 4 minutes into the video, I think Paul clarifies that vacationAllocated - vacationTaken is a "getter" (code that reads), therefore, is it correct for me to assume that vacationRemaining is a closure that is a getter?

I'm still kind of confused as to how getters and setters work after this video but all I know is that one reads and the other writes. Getters take the value and the setters edits the values (dynamically?)

I also can't seem to wrap my head around this line of code as well:

vacationAllocated = vacationTaken + newValue

Why do we want to change the vacationAllocated when it already has an initial value that is set to it?

I'd also like to apologize in advance if my questions sound really stupid but I really just can't wrap my head around it and I'm really struggling to understand such simple concepts.

1      

A new hacker has a question about closures versus computed properties.

I'd like to clarify whether var vacationRemaining is a closure. Thank you!

Nice question!

While the syntax is similar, here's an Xcode tip to help you see that vacationRemaining is a variable, not a closure.

Tip: In Xcode, Hold the option key down while you hover over an object until you see a Question Mark "?". Then click! Xcode will display some documentation about the object.

So, if you option-click on vacationRemaining, what does it show you?

While both are defined in curly braces {}, the computed property is used like a variable in your code. Here's an example for you to try in playgrounds.

// Run this in Playgrounds!

// Simple struct to help plan a vacation
struct vacationPlanner {
    var availableDays   = 42  // The ultimate answer
    var plannedDuration = 14

    // This is a computed property
    var vacationRemaining: Int { availableDays - plannedDuration }

    // This is a function.
    func isVacationPossible() -> Bool {
        vacationRemaining > 0 ? true : false
    }

    // This is a computed variable.
    var isVacayPossible: Bool { vacationRemaining > 0 ? true : false }
}

// Setup
var bahamaTrip             = vacationPlanner(availableDays: 20)
bahamaTrip.plannedDuration = 6

// Note: This calls a function that returns a boolean
let tripPossible_1 = bahamaTrip.isVacationPossible()

// Note: This asks for a value (a bool), that's calculated on the spot.
let tripPossible_2 = bahamaTrip.isVacayPossible

// Find the remaining vacation balance
let summerFunDays = bahamaTrip.vacationRemaining
print("You could spend \(summerFunDays) more days on vacation.")

// Option-click .isVacationPossible()  and .isVacayPossible
// Xcode will show you one is a func and the other is a var
// Option-click .vacationRemaining  <-- What type is this?

1      

Hey man. Thanks for the option-clicking thing. It was pretty neat!

So, if you option-click on vacationRemaining, what does it show you?

Option clicking on vacationRemaining states that it's a var vacationRemaining: Int { get set }

Therefore, it's a getter setter?

Option-click .vacationRemaining <-- What type is this?

var vacationRemaining: Int { get }

Correct me if my understanding is wrong but is it read as vacationRemaining is a variable of type int with a getter? (It's neither a function nor o closure but simply an integer type with a getter inside that allows as to read values?

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.