How to compute property values dynamically – Swift for Complete Beginners

preview_player
Показать описание
Other videos in the Structs section:

2. How to compute property values dynamically: This video

Рекомендации по теме
Комментарии
Автор

Hadn't understood this concept right until I found this video, thank you!

MiguelFajardo
Автор

Does this still work this way? I first I tried to follow along with the Video, coding along. It didn't work. Then I copied the code from a screenshot in the video and it still doesn't work. This is the error message I got: Expected 'get', 'set', 'willSet', or 'didSet' keyword to start an accessor definition. If somebody did this recently and it worked, just an answer telling me that it still worked for you would help. Thank you!

mattesbieler
Автор

I understand how the code works, but the vacation theme and how it's explained made it confusing for me. Why would you use the vacationRemaining computed property to change the vacationAllocated value.

I think it'd be easier to follow if setting the .vacationRemaining calculated the vacationTaken value, ie). set { vacationTaken = vacationAllocated - newValue }. vacationTaken and vacationRemaining are the values that are linked, the more you take the less you have remaining and vis-versa. Change one and it affects the other changes.

Bit-Buffalo
Автор

Why would you want to use a setter here, rather than a mutating function on the property?

NathanBudd
Автор

Hi, I have a doubt. Why I should write {get} or {get set} next to var value? I see swift must need for run my code.... but why?

RavilDaud
Автор

I have a suggestion. Why don't you just write

vacationTaken = 4 instead of using += operation. This will mean the same, isn't it? Because the struct already has the initial values (default values) for both vacationAllocated and vacationTaken.

sagardaniphotos
Автор

Maybe I'm missing something obvious here, but why are we changing the allocated vacation with a value outside of what was taken? If he took 4 days of vacation, he should have 10 days remaining. When running archer.vacationRemaining with no value, I get the right amount. But also, why are we setting the vacation allocated every time when theoretically that shouldn't change?

jonathankane
join shbcf.ru