|
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:
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:
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. |
|
A new hacker has a question about closures versus computed properties.
Nice question! While the syntax is similar, here's an Xcode tip to help you see that
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.
|
|
Hey man. Thanks for the option-clicking thing. It was pretty neat!
Option clicking on vacationRemaining states that it's a var vacationRemaining: Int { get set } Therefore, it's a getter setter?
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? |
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.