Computed Properties in Swift

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Watch. Thanks for sharing. Really well put together tut.

👍︎︎ 1 👤︎︎ u/jcnh74 📅︎︎ Feb 03 2020 🗫︎ replies
Captions
the purpose of this video is not to be exhaustive in my explanation but rather to get you started thinking about a better and cleaner way to build your code once you've taken these initial steps you can start following and watching people like John Sandell who takes these concepts to the next level I'll leave a link to his site in the notes below as I was developing my skill set I read a lot of blog posts and watch a lot of videos but I found that often either a the writer or youtuber often tried to cover too much and that get lost or B the blog or video quickly went over my head in this video I'm going to go through an example where I refactor the kind of code that I used to do as a beginner to utilize class and struct computed properties this is the first in a series of videos where I go over computed properties property access levels and property observers if you're interested in learning about this then just keep watching I'm going to start out in this playground to show you how I used to do things I'm going to create a simple employee struct that has four stored properties the name annual salary number of weeks per year that he works in the number of hours per day that he or she works in the past if I wanted to calculate the weekly salary I'd create a function that would accept the annual salary the number of weeks worked and returned the salary based on this calculation similarly if I wanted to calculate the hourly wage I'd pass in the annual salary the number of weeks worked the number of hours per day and do my calculation like this so let me create an employee whose name is Stuart who has an annual salary of 120,000 per year I wish worked for forty eight weeks per year not bad vacation and for seven a half hours per day and get his weekly salary and hourly wage bypassing the corresponding employee values into each function that's served my purpose but since my functions are always going to be applied to an employee what are they doing sitting outside of the employees struct they have no business being there with computer properties you can hide the internal data and functionality of an object the computer property is one that runs some code in order to calculate the value this is known as encapsulation and is one of the most important object-oriented design principles only the employee struct needs to know how to calculate the weekly and hourly wages so let's bring those functions in by way of a computed property I'll just duplicate the existing struct and rename it employee to now I'll set up a computed property for weekly salary and it'll be of type double they do this by declaring the variable and type and then creating a code block that returns to calculate the value in this case annual salary divided by the weeks per year as a double I can do the same with hourly wage as you see a computed property can use another computer property in its code block and as well as any of its stored properties this computed property is known as a debtor I could write the same code block like this but since all we're doing is getting the value that can be left out but I'm gonna leave it here for the next section let's test this out by creating another employee using this new struct Aidan salary is $50,000 a year and he has 50 weeks to work for 8 hours a day getting his weekly salary is easy we just have to access the computed property on the instance of the employee - same thing with hourly wage now what if I wanted to change the hourly wage an employee and have that update the annual salary if we try this on Aidan employee - we get an error cannot assign the property hourly wages it get only property let's see if we can fix that let's copy and paste the employee to struct again and make a new one called employee 3 by default all computer properties have a getter as I said in the last example but they can also possibly have a setter you can create a setter that can modify other stored properties of your struct when you change the value of a computed property that has a setter you have access to the new value so we can use this to calculate the annual salary and update our salary property so if we create a new employee Emily whose salary is twenty-two thousand five hundred a year works fifty weeks per year for seven and a half hours a day we can as if before find her weekly salary and hourly wage but now because we have a setter on hourly wage we can update it and see how not only our annual salary has been changed but also the computed weekly salary is also updated in the next video in this series we'll take a look at class and struct methods that can also be used to modify your properties but we will also look at public and private access and how we can limit how those values can be manipulated I hope you found this video useful if so please give it a thumbs up below and subscribe to my channel if I get enough positive feedback I'll continue to build out similar tutorials for Swift developers who have left the starting gate but still need to add to their toolbox you can check out my youtube channel to see what other videos i've created visit my website to see my iOS app portfolio of apps currently on the App Store and check out my github repository to see what else I'm up to thanks for watching I'm most active on Twitter so follow me there for notifications of other Swift related things that I'm up to
Info
Channel: Stewart Lynch
Views: 2,882
Rating: undefined out of 5
Keywords: swift, computed properties, xcode, app development
Id: yze92fm54vU
Channel Id: undefined
Length: 6min 39sec (399 seconds)
Published: Sun Feb 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.