Swift: Master Swift Properties in less than 1h | Swift Next Step | Skillshare

Swift: Master Swift Properties in less than 1h

Swift Next Step, Swift Expert|Senior iOS Developer|Udemy

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
6 Lessons (59m)
    • 1. Swift Learn Properties - Introduction

      5:04
    • 2. Swift Learn Stored Properties

      5:49
    • 3. Swift Learn Lazy Story Property

      6:28
    • 4. Swift Learn Computed property

      18:59
    • 5. Swift Learn Property Observers

      21:45
    • 6. Swift Goodby

      0:41

About This Class

Swift properties is tricky, therefore, it is very important to have a solid understanding of Properties to master Swift

Properties are a fundamental part of programming and it is a MUST for any programmer. This course is all based on the FREE iBook from Apple that can be found in the apple store called "The Swift Programming Language". It has 561 pages of Apple’s "super simpler language", but before you run and download this ibook, let me tell you what I will offer you in this series.

I read Apple's book cover to cover and I went through all the examples. I talk with many Apple evangelists and today I am what they call an "Apple expert". However, I explain in plain English (with accent) with easy, simple, and everyday applicable examples. And if you watch the videos, follow the examples, and still having questions, I am always here to help you.

You have two options now - you can either download Apple’s eBook that explain properties like this and have nobody to ask if you have a question:

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.:

"Properties associate values with a particular class, structure, or enumeration. Stored properties store constant and variable values as part of an instance, whereas computed properties calculate (rather than store) a value. Computed properties are provided by classes, structures, and enumerations. Stored properties are provided only by classes and structures. Stored and computed properties are usually associated with instances of a particular type. However, properties can also be associated with the type itself. Such properties are known as type properties. In addition, you can define property observers to monitor changes in a property’s value, which you can respond to with custom actions. Property observers can be added to stored properties you define yourself, and also to properties that a subclass inherits from its superclass.

Stored Properties

In its simplest form, a stored property is a constant or variable that is stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the var keyword) or constant stored properties (introduced by the let keyword).”

What are the requirements?

  • Basic Swift programming

What am I going to get from this course?

  • Over 9 lectures and 59 minutes of content!

  • Learn in full detail about properties in Swift

What is the target audience?

  • Anyone with basic knowledge in swift language

  • Anyone that wants to excel in Swift programming Skills

Transcripts

1. Swift Learn Properties - Introduction: a lot of one and welcome to Intimate Swift 2.1. Siri's that explain. Do what this series is about. This series is based on the Swift programming language 2.1. This is a free a book that you can find on iTunes and you can find on my book for free. It's about 561 paste. I read this book. Cover to cover are explored. I tested how the cold that offer on this book and what I'm doing is I have currently three course. I have about 1600 students on my course, and I know what the problem my students has been having in what are the main questions they ask me all the time. So I decide to get the main chapters on this book that relates to the questions that my students commonly asked me in. Make this transforming these chapters into course. And why did I do just set small core. So let me show you the course I'm gonna be offer you. So each one off This is a chapter. So prompters, additional methods, closures, particles, irritants, extinctions, generic un's instance, life cycles in type assistance. So I'm gonna be doing one course for each, and the reason behind it is not. Our students have problem A off the subjects, not all of the need to. For example, some people have problem with professionals, but they grated properties. Orders have problem with protocols, but they greater generics. So there is no reason for is no one side fits. Also, you need to find out where you have problem, where you need to address and look for the course is gonna fit your best. At this, cars were gonna be seen properties. That is where heavy seeing lots of people. We have lots of problems, and that's we're going to start. I'm gonna be followed already being seen here. And this course we're gonna be seeing start prophets, getters, setters, computer properties. We're gonna be seeing much more. Actually, we gonna be seen story properties, lays storied properties, read the only computer properties, computer properties with getters and setters propped up service. We're gonna be seen. Will set indeed set. So this is what we're gonna be seeing on this course. So this course there gonna be short, but they're gonna be going deep in each topic. I think if you have probably properties you gonna love if you don't. But do you have problem with some of the other topics? Like professionals, methods, cultures, protocols? You definitely must come back and check it out. This code, this other course when it come out because they're gonna be exploring how the details that's available on the book from EPO and definitely is gonna be a great thing to have in your arsenal for future reference and for any case when you need it are, you know, helping hand as well. I will be here. I will be behind this course. I will be giving my support and I will be helping you. As always, Discourse has 30 days money back guarantee. If it This is not what you're looking for. If you don't think this short course is the answer for you, you can always Cramer money back. No questions asked. No hard feelings. It's OK. I understand is a trial is a trial for me Israel. So I hope I see inside. Of course, I hope. Hear from you. What do you thing? Let me know and let's have a chat. So rest in real Now what's the first lecture? Seeing a message and I'll see you inside 2. Swift Learn Stored Properties: Hello, everyone. So in this first lessons about properties, we're gonna be seen store property in what it is a store property. Well, I stole property is nothing more than a placeholder for us to store. Ah, value that we're gonna need to use after. So it can be a constant fell when we use let or it can be a there boom failure when they use the word. So I So when we have a class structure are in, um and we declare something as a buyer or as a light that is a store property. So that's your example here. So here I haven't distract it's a person And this has three story properties. Won his first name. That is a string. Another one is less name that is also strain and the other one is a nickname. It is also string and you can see the two first ones. They are declared as a light. That means this person can never change its first name. And it's less name because there is makes no sense for one person to change its first name in its last name. You cannot change her for same in your nest name however, you can change your nickname, so your nickname is of our because it can be changed. Destructs give us nice initialize er's. So when we declare person I struck create initial either for us where we can best valuers from our store properties so you can see I'm declare e a person. Give us the first name is Nico. The last name must love a dirt and the nickname as instructor. The Domenico person don't nickname. You can see on the corner it says instructor. Then I'm changing the value off structure to self destructor. And then when I'm asking again, the new parents and up nickname it give me a self destructor. So that's what the store properties are. Now let's go and have a look on some example, and that's exporter. A little bit more story properties. So I have here and they already have the code on, so I don't have. You don't have to watch me time, and I will put the coat on as well for you so you can download the code for class and I'll put a zip file with all the codes, and I also put out the individual codes so it's up to you What you prefer? No. So here let's explore a little bit and see We have this chapter again and we have the person. And if I just hit, enter and I run. We have accepted the same result here. So what I want to show to you is it's a try. Changed the nickname here in a church, change my first name and say, Well, my name is not Acre anymore. My name now is hero. Yeah, I'm hero and it didn't say their safety is gonna complain and say you have to change lead Tovar to make it multiple. So it's saying that she is even great out here. Jeremy, this has to be Vier. But it's not the case. I just want to show you the difference. So we want this to be a nickname. I need work Finals instructor. Also, I want to show you this fire person. It's a viral because we're changing the nickname if I would go, the clarity is is a lead. It worked fine until I tried to change the person. My nickname. If I didn't have any reason to change my nickname, I could declare the new person as an instructor and because my star property would always be the same instructor value than this doesn't need to be a bar, it could always be let. So this is what I want to show you on this class. It's a very simple class, however, is the base for everything. We're gonna be seen on this course, so it's very important that you understand this very well. So if you have any questions about anything, I explain on this lesson, Please let me know what other discourse board, and we can have a chat about it on the next class. We're gonna be seeing lazy, stored properties. That is very cool and very important as well for a good application. So I'll see you in the next class and have fun. 3. Swift Learn Lazy Story Property: Hello, everyone. And welcome to this lesson about lazy, storied properties. And what we're gonna be seeing on this class is how we usually start profits to make our applications even better. So what is laid? Started? Profits, Celester Properties is Robert is that we created. And they're gonna be, you know, application, uh, dormant until we needed it up dead bearable to be initialized. So let's look into this example that I prepared for you. And what we have here is at that importer. So this could be, for example, let's say we have a user where it generates a profile where we have a simple image and then some data, like, use their name, um, email, data parties, whatever that is. And then when you click it on the image $8 a very large image with Marty Tail, so we don't actually want to go. And now that very light agreement, thank you is actually necessary. So how do we do that? Oh, this case I have here doubting Porter where you see a head, I'm fair, Boco that a manager value would say doing intensive processing. And that's our lays. Come story property. There's just gonna be initialized and you can see it. But I need there would say I finally got initialized because we're gonna see when are executing that. That's just gonna peer when it's actually necessary. So business part of data Manager that is, has out the order, Doctor, Where could have the user name? These are birds and out our detail. And I put I need stairs without it say's I got initialized and and and then the code below is just I'm initialized that manager, calling some data from the major in front of that important. So that's basically the theory that's jumping to the playground and see how that actually is working. Okay, so here on playground and what I'm gonna do, I'm gonna comment it out. This part of the coat and if I initialized is variable, we can see that say's already he. So it's even coming this part out and you can see nothing's happening. So that's all that creative, a very become manager. It's the type of dot a manager I would say I got to initialize gets cold because I created forever. And then I can go ahead and access the value off manager. The daughter that say's access that to adult initialized at the manager and I can actually access to value. You can see here I'm accessing the value. But you see, that important documentary parted would say doing things processing Didn't God in its allies it all yet So when I called it value and you can see that the very brook is declare here inside automate, that's very important to notice because it's a okay, you're not calling anything inside here. But it is declared is the same thing that I'm doing here. See that the manager here? I'm not calling that here. And that's what is important to notice. Are not calling anything inside that The manager here not calling data, Let's say I got initialized. See, I'm not calling idiot, So when I call it Well, that was already initialized anyway. But here I'm already declared that very about the semi did here. But this didn't go initialized. However, when they go ahead and then I access that there. But now I need that variable No x cords going to go there. It's got initialize for me. So now I have access to the value. But now the very well got initialized. So this is how plays storage property works. I hope you understand is a more little bit more to the advance. It part is very useful. When you don't wanna your application to be using data that's not really necessary. Especially if your applications using at work, especially if you're uses on three G. You don't want it to be using that work that's not really necessary. You don't want to be is in battery and things like that. So that's when you use it. But as well you need to be thinking about it. You don't know your used to be waiting as well for too long it when you needed. That s so it has to have a balance. So if you have any questions, just let me know and I'll answer assume as they can. As always, I hope you're enjoying this and have a good day 4. Swift Learn Computed property: Hello, everyone. And welcome back in this lesson, we're gonna talk about computed Properties. We're gonna talk about two different types of computer properties we're gonna talk about, read Onley, computed properties and about computer properties that have getters and setters. So let's start talking about what is computed properties. So computer properties are failures dead there? No, actually stored into your first different in the story properties there were just so before we calculated the value the property will return based on the I started property or not, and give that value back. So, for example, if we have, um, the area off a square instead we have how started property to save value. We know how to calculate it. We have decide off this cool off there, square, we can just multiply, decide by the other side and return it. So we're gonna be looking someone examples and how we do that. And when we use it and how we use it, there is just a note. I want to bring it on. This is actually from day F o r. Book that we have been using and I normally don't use read all my presentation and when I use you should pay low reputation and says here you must declare computer properties included that read Onley properties as favorite properties with fire keyword. Because Dave Ire, the value is not fixed. So that's just have a step back. And this thing with that saying so it doesn't matter whatever they start property declaring , you always gonna used the key word far in front of it. And I'm gonna show you when we going to the examples. But that's a must It is always eyes never elect So it took an example here. Very simple example. In this case, I have a struck co person. And then what I do is I have the first name. That's a start property. I have the last name. It's another story property. And then I have a say, Hello. That's Ah, computer it property. And what it does is Saiz. It returns Hello and then the first name and then the last name. So it is very simple, very, very good example to learn. And this is every only computed property. No, let's have a look in her much more complicated case off the computer property. Where has getters and centers And this is an so an example from the airport book where we have, um, square rectangle and we gonna coca at the center off the rectangle. So in this case, we have three different structures. One is a point. So the very 1st 1 in the dog with a X and why then the 2nd 1 is the size with the width and height, Then the 3rd 1 is the actual tango where we're gonna use the point and size are the story properties and then we ever center in their ST is gonna be our computer property. And when we set and when we get the value off that point, we're calculating it to store in, to retrieve. And I'm gonna go too much over here. I'm gonna go a bit more deeper and to the project otherwise is gonna beat your difficulties to keep showing you what's happening. But basically what we're doing here is equating a point and zero recreating retain, go with size 10 or heights 10. Then we are creating the this new retain Go are initiating getting the center and just changing his numbers. We're gonna go over this, uh, now into the code into the parade ground. What we're gonna do, it's first of one to step back and go back to the first exam for that smart simple. I understand that first example. Then we go back to this example and we're gonna understand. And we're gonna understand line by line, this example. And by the end of this class, we're gonna know everything about computed properties how to use going to use it. So let's go and have a look. Here is the example on the playground it is available to you. You wanna follow along in the The very run is very small here. So we have this structure with the first name last name and it returns this value that say's hello first name, last name. And here I create my name. It's a cold person. Don't say hello And it surely course Hello, Pecoraro Over there. And well, there is no month secret. I just want to show you the problem with the let keyword. So it's already say here, let decorations cannot be computer properties. So exclude woman todo any mistake its collection of arm in, uh, I think that it's pretty much it, but if you have any questions? Just let me know. Let's go to the second example. That's much more complicated. Well, it's not really complicated. It's just a little bit more long. Let's say it's OK. First we have to his tracks. Okay. Our first truck during the point her second struck Jaryd's with and the height in our third struck carrots. The rectangle Let's forget about they computed property for now. So with this to data, so we first we create their point. So we're not using the return you yet recreating your point on 00 Then what can we create? Our size Clinton And now we're creating are a tango. So we didn't touch this yet. We haven't retained him, and it's 0.0 size 10 thing. Now what we're doing here now is the first time we're touching the computer property. And this when we assigning a value when the computer property is on the right side off the equal sign, we are using our get her. So you're getting this very and signed something. So you're coming over here? Okay, This is very important when we're using the dog notation. So the documentation is very well name dot Something were using to get Okay, So Okay, we are using this and we're greatly and your variable with square dot center. So, what that means Let's have a look. If you hold the key, the out key, See how my cars returning to a question mark. Now I can click into that, and it's gonna tell me exactly what happened. So my initial square center is gonna be the type point. Why is gonna be a type point? Well, we're calling my square dot center. In my square is type of re tangle. Retaining the saint is type point. So this my scar got center is returning point. Okay, so now you know what years attorney here. So this is returning a point. So now if we look here, so you know and they can see your return point and what's the point? It's return. Is is returning, appointing with center X and its center. Why, what is in their eggs? Center X is orange dot Exe in what's orange About eggs where we know the orange that X is zero. Plus I start with we know that size or two it is then divide by two So that's put out this data here so you can see origins, some space. It's got it or in your thanks signs up with It's been this close together. Okay, so and now that's what aim of the variable. Not now. We should be able Asada's X called finishing computer everything. We should be able to see what is being calculated here so I can say orange dot x zero sighs don't work is thin, and that's even put it. I know everybody knows mats, but let's everything here and then size up with by to its show five. Any time now. So what are we getting? How are you getting ice choir dot saying there is calling. Get this. Getting zero plus tens right by two, then is getting the same exactly, very. For why it's returning here. This initial square center. It's gonna be this point. Valued that this then then sorry. It's 55 Then we are creating a mule my square dot center, and put into the 0.15 15. Well, now we're doing the opposite. No, my square dot ST er is going to the set because the dot center is on the left side. off the documentation. So what happens here? New center. It's going to receive this point. So New ST is gonna have 15 and then sized up with we know that is we know that the size is stand right by to, you know, that is five. If we passing 15 we know the size off. We know that they knew X is gonna be 10. So the new orange dot exe is gonna be then. Okay, this is already printing here. The square orange is now 10 10 But let's again have a look and see why. So if you will If we bring here just to show again that my square don't our Jane don't eggs in where? Exclude computer days. We're gonna go and trace what's happening. So my so my square That center zone, the left side We're adding the value off 15. So is cut. Is calling the setter with this new center, that is a point and develop Texas 15 in the years subtracting the with that stand divide by two. That means it is five 15 months, five with 10. We added that valley to the origin, so there are new variable origin now is 10. So now Xcor finish computed my script or in X is Stan and I put here Cem off the other values just so we know. So this is how getters in setters work is. I think the most important thing here to remember is get is when his own the right side of the equation like it's in here. So I'll edit. Yeah, this is good, you know, here, this is it. Just to be sure and then as well to remember, this value is not getting edit to the origin is going out off the function, going out off. Distract. This value is come inside is being manipulated. It's being headed back to the origin is going edit inside the store procedure You could have if condition here it could be conditional. So you could say if it is to origin is negative, then ignore doesn't save it so wouldn't change your start procedure and would always be posted various. But otherwise, if you don't have a condition, the value gets saved in your extract get modified. So you have to remember that as well. So this is what I had for disc less for you guys I hope you enjoy, as always you have any questions? Let me on the discussion board. I will be replying to you. It's was a can in any comments and suggestions, just let me know. I want to hear what he had to say Otherwise I'll see you on the next lecture. Have a great day. 5. Swift Learn Property Observers: Hey guys, and welcome back for our last kind of properties that property observes. And the probably observers a coming to different flavors that will set indeed said, in the main difference is just the time where they get cold. The problem observers. They can be used for many different reasons that they're very handy. They can be used Phase two chained the way that their application works and they can be used for debugging purposes are used a quite a lot for many different reasons, and we're gonna be over going over few of them on this lesson. So what are property observers? So they basically they was falling to change to a variable. So they have this two different favor that I told you they will set and did said so The will said We'll be called before the variable get changed and book give you a reference to the new value as well, while the did said will be called after the Vero get changed and also we'll give you a reference to the old verbal. So let's go ahead and let's have a look how it works. So I have example cold here and this code come from the EPO book as well. Where is this is I stepped. Countered s. So there is a class step counter that's initialized with your steps. And every time the number of steps changed it, just give it on on the wheel said it. But the message say about you said Steps Jew and then the various off the step in the did step it just do a copulation to tell Harman steps gonna be edit from So it's a term does the total step minus the old value of steps until how many steps going to be at it. So let's go and jump into the code and I'm gonna show you how do I normally use this property observers? So I have my pregnant over here and the code is already here so you can get this code own the files to download it on, you know me and follow along. I just put it already here so you don't have to watch me type B, but basically the same code. You just so, uh is I stood counter class. We have a variable and this variable has to Observers will set and did set. So, like I told you that will set. It's called before the very begets said So it gave us, um, reference to the value that will be set in the Israeli sculpt new total steps. But this is a value we can give any name we want in this case is called no total steps. But we could call if you want just new value. And of course, here would be new value. So it's just whatever the name we put inside here when we called new Well said, Still, very, we have two years here, okay? And I would value as well, um, when we call, did set. We didn't person value, but if we call here with old, then we just have to call here coat and owned then, um Exc Rx could know that when we called it set old is different to the old value that the variable head. And if you don't do best any value, the default value is old value. And if we hadn't pressed any very here, the default value would be Newbury. We didn't have to use new very of year. That's how it works. Okay, Now we know that we can go ahead and test. So we have step counter, we create the variable. So if we go and check the value off step counter the third her steps, I see zero. So now that said Total steps to 200 and this was accepted. 200. We see well said we'll get called first and say about said to the steps that you 100 and then added to her the steps and this 200 steps came from total steps miners, old state failures. There was zero. So 200. And if we dough step count again, it's gonna be, of course, 200. No, let's go ahead and edit another 200 steps. Sorry. Um, let's go here. Oh, you're at 160 steps. Sorry about that. No, what's happening is we're calling. They will said in now, saying about to set daughter step to 3 60 So it doesn't really matter What old beggar Waas just setting total steps to 3 60 no good and said is just adding value 160. So it's going to the steps. Waas 200 old value Waas Sorry to understand history. 60 old value was 200 Toto 1 60 Let's print Israelis here one by one so we can actually see total steps. Oh, hey, how we have it printed two times. So there you go. We have first time. 200 first time zero, then second time. Click it, then we have for the second time sort of steps. 3 60 out. Value 200. So photo edit 1 60 So there is the explanation over here. And then again, for the less round we can have 186 assert 896 and go About two said Toto values to 896 Then they say, Edit 536 because 896 Mine's 36 year is 5 to 6. We could also add it's, um, logic here where it say's Well, what's the person give more than 1000 steps a day? We going to stop logging and say hair arrest? So when with we'll set here Okay, we can do a lot of here and see if no radio greater in 1000 then no better equal 1000 praying. Have a wrist. Sorry. Here, of course, is not no value here would be just toto steps. - So let's say if you want to put a condition where the person can no walk more than 1000 steps a day in if they dough. We should print the mass urgency. You're way to walk 1000 steps, please have a rest and stopped counting it. So we can every condition here say if well said greater than 1000 then And this, uh, we'll set is immutable variable. All right, sorry, not well sake. - Yeah , so let's say we wanna set the limit and we don't want the person Teoh have more than 1000 steps that they are. We want to stop counting after 1000 steps a day. We could add a limit here and say, for example, a faith. And then the new value created and 1000. And it's important to notice that it's very here. It's ah, lead. So is a constant and to say to check here again, I hold out and that's from this question. Mark and I click on the variable and that show the decoration. It's a let is a constant. I cannot changes value, but the value off this is going to go inside this variable so we can access is really use itself the total steps and accepted to 1000. So that's the mess. Immel value that bearable can have an ex sprained. Now you reach. Okay, Toto. Mount Oh, steps. Please have a wrist. Now until we come here and try to set his steps to 1000 two hundreds and try to read it. - So what you can do is if we wanna set a limit for desirable When let's say we don't want toe. They've able to count it over 1000. This very this very real said it will always happen. So there is nothing we can do here to avoid it. So we waited until it's get set. And then once it did get said, we go and we re set back to the value we want. So after he did said we go ahead and Barbato is we say here before our this large Chrissy, if oh, baby, um total steps because of your interest in the amount of steps, is created in 1000 then and now. But though is how and out its text. And to do this I select everything and held control and I exclude her. Do this for me. So if the amount of sepsis greater than 1000 actually, I want to do it is not greater than 1000. Then it does all of this else. If it is greater than 1000 then I want to set bake to 1000. No one else sprained. You sheared Have arrest? No. Now, if we go ahead and trying to set the steps to for example 100 and try to read gay amount of steps through through the steps to 1000 and say here you should have a rest Now to be easy to see. I can stop out the others on start steps from the 1st 1 And we had 200 steps. So we have here steps 202 100 steps now. Oratory, Ed. If we said steps to another 500 then it gets Call it and this Get call it. You should have a rest now. Individual steps. It stops in 1000. And every time we try to add more steps, it's gonna get Call it too. Three or times in the number of steps there is gonna be over 1000. And what? That's good for debugging. I was saying so for example, we could have a variable that's getting said. You don't know what's getting said You can best value off the self that's changing the value. And every time the value very well get changed. You can edit break points there you can print in the various off the variable that's getting changed, so you can see so you can add print lines. And then you can ask her, Did he bugger here? And you can see all the detail here under the bugger and you can, uh, easily find ho is changing your variable that otherwise could be very difficult to find. So this is private observers, I think is one off my favorite properties. Um, she is. He helped me a lot. Sometimes I put it property observers to help me debugging Um, there is like I said, meal, about 1000 reasons to use. You can use a during development, and then you can take it out. If you need Teoh or it can be part of your cold, it's up to you. So let me know how you pretend to use it. All this properties that you learn let me know how may enough disproved sinew for you. And how many is gonna be part of her arsenal? Let me know as well if you have any questions. If you think I miss any property, if there is any propped it to here and that you want to know about it and that will prepare some slides in some classes to teach you. 6. Swift Goodby: Hey, guys. So this is what I had prepared for discourse. I hope you guys enjoyed. And I hope you learn a lot. So let me know. I'm really want to know what you think and really want to hear fit bag. Now that you've finished the chorus, please remember to post your fit back here. We feel Give me your suggestion. So the next course can be even better. And yeah, let's keep in touch, I hope. Hear from you. I hope he your ideas in have coding and see you on the next course.