How to take action when a property changes – Swift for Complete Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] swift let us create property observers which are special pieces of code that run whenever a property's value changes these come in two forms it's a did set observer that runs code after the properties changed and will set when it's about to change to see why it's useful first look at this older code without property observers we have a struct called game with one variable property called score equal to zero by default we then make one of these things then we go ahead and use the game we'll say add 10 to score score is now whatever scores -3 scores now whatever scores plus one scores now whatever so we change the score we print the score change the score printer score change the score printer score whatever we change we print except as a bug i totally forgot to print the score at the end there so there's only two prints and there's three changes that's a mistake we want to print the score whenever it changes now with property observers we can prevent this kind of thing happening we can actually attach that print call directly to a did set observer on the score so whenever the score changes it triggers dead set and prints it out to do that we remove the print calls from the main body of our code and then squeeze it all up we'll then make space inside our game struct and attach the observer to score like this there's an open brace and did set print score is now score and with that in place we get access to that print call whenever score changes whenever in our code wherever in our code it will always update it's really really nice now if you want to swift automatically provides a special constant inside set called old value to read what the score was previously plus there's a will set equivalent when a score is about to change you assigned it the code assigns happening it calls your will set and the code to assign completes and that will get a new value so you get old value for dead set new value for will set we can actually show all this functionality off in action using one code sample which will have various messages being printed so you can follow the code flow with actual execution in your playground we'll say this struct app has a contacts array of strings inside there we are will set observer with print current value is contacts oops and then print new value will be new value like that so we're seeing as we're about to change values here what's the current value and what is the new value we'll also add in our did set print that are now contacts dot count contacts and we'll do print old value was and again we can use the old value constant to read it out so that's our new app struct with various print messages happening we can go ahead and use that we can say var app equals a new app app dot contacts dot append let's do daisy append adrian e and then we'll do w and s my posse and then uh yes by the way if you're curious appending to the string array will trigger did set changing the array so it should print lots of text let's find out boom so up here we have current values empty array new value will be adrian e and that's our will set observer running because we're trying to append adrian e then we have old value there are now one contacts and the old value was empty array that's our did set running they have the again uh the will set and did set and again we'll set and did set so it's triggering all our prints as you can see so you can really follow the flow through now in practice will set is used much more infrequently let's say much more rarely than did set but you will still see it from time to time because particularly in in swift ui sometimes when you announce changes hey a pair an animation you want to use will set so has time to prepare the animation correctly before you do any changes regardless of which you choose i would ask you to try to remember don't put too much work inside your property observers you know if we've had game dot score plus equals to one that looks to us we're writing it as a trivial operation but that triggers intensive work or network requests or saves files or who knows what it's going to catch you out on a regular basis and cause all sorts of performance problems you
Info
Channel: Paul Hudson
Views: 5,083
Rating: undefined out of 5
Keywords:
Id: pfJL8AW8UVQ
Channel Id: undefined
Length: 5min 25sec (325 seconds)
Published: Sat Oct 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.