How to use Structs vs Classes in the Swift Programming Language

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's up everyone mark here and I got a good question the other day what's the difference between strux or structures and classes and Swift so we're gonna cover that a little bit today we're not going super in-depth but just at a basic basic level here so I'm gonna create a new playground link we're gonna call this Struck's and classes i'll make this fullscreen here so one should use a structure one should use a class it's a good question first off right out of the gate let me tell you apple's recommendation is always use strux as a first choice and why is that well there's lots of reasons why first off Struck's are more memory efficient okay they're gonna actually save more space use less memory and they're a lot faster so structs are faster okay pretty cool stuff right and another thing to keep in mind is Struck's are value types or as classes are reference types whoo that's really interesting ones one's a value type and one's a reference type okay so a class is actually holding a pointer or a reference to an object in memory whereas a struct is actually a value type it's holding data and when you when you make duplicates of it or rather when you assign it into new variables it's actually making copies whereas classes aren't so let's go ed and demonstrate this here and we'll talk more about when you should use one over the other so let's go ahead and create a struct here and let's call this person and we're going to say var name of type string var age of type int and of our weight of type double okay and let's go ahead and just create an instance of this now when I create an instance of it it's creating a copy in memory okay this will not create a variable here so when I say var person a equals person it's going to create an object here in memory it's not an object though it's a structure it's a value type we typically say the word object when we're referring to class instances so name we'll say Jo age 25 24 and weight is 200 and then what I'm going to do is I'm going to say var person B equals person a maybe you've done this maybe you're brand new and you're like I always wondered how this works what happens when you actually assign one object into another well it's different if you're working with Struck's or classes so let's go ahead and do an example here so I'm going to say print person a dot name and print person B dot name I'll just run this here so they both say Jo so this one's Jo this one's Jo right because we took a new variable here and we've assigned it to a but what's really happening here under the scenes well let's find let's figure it out a little further so if I say person B dot name equals Lydia okay so I'm gonna change the name of person B which was assigned from person a and if I say print person B dot name but what would we expect to happen well we'd expected to say Lydia now let's go ahead and print person a dot name so you notice that when we change the name of the second object person B it did not change the name of person a this is critical one of the rookie mistakes rookie mistakes that new developers often encounter why did this change all right it does not change it did not change the value of name and person a and that's because structs are value types so when you assign them into a variable it is making a copy or when you assign them pass them in through a function as a parameter it's making a copy every single time a copy of the data it is not referencing this same object in a memory not at all completely new object which is why this changes and it works now I'm gonna show you classes now I'll talk about advantages and disadvantages of all the stuff in a minute but I want to show you what the actual differences are so let's say class and we'll say human instead of person just to keep the name different and I'm going to save our name of type string save our age type int and bar weight of type double okay it's gonna yell at me here because there's no initializer so we need to do that so struct okay these initializers are these variables here there's an actual initializer inside this happening automatically if you don't define one these are going to get initialized or you it's gonna create an initializer automatically I mean so it's gonna force you to put that person in there it put this data in here I couldn't just save our person C equals person like it's gonna require that I put in these required variables but not necessarily here inside of a class and so it wants me to implement an initializer so it has some different options here stored property name without value prevent synthesized initializer so we actually have to give it in a class we actually have to give it a value so one thing I could do is I could say equals an empty string to initialize it because a class needs to have an initializer or it needs to be initialized so let me open this again here if I click fix see how it's adding that what I just did on there or what we do is we create an actual initializer and I can say name of type string age of type int and weight of type double so there's my initializer and I'll say self dot name equals name self to age equals age and self dot weight equals weight for those of you were absolutely brand-new and we've never seen this before just so you know this could be named anything these parameters I could have called this Bo law and then over here I would just say self dot name equals blah okay these are just parameters alright without the age these are just parameters here okay but typically the the style of convention is that you just give it the same thing because this has local scope so when i say equals name it's referring to the one that's most local here and that's why we're using the self keyword here okay if this was actually blah like so I wouldn't need the self in here I could just say name equals blah and it's gonna refer to this outside name but this is the convention here just to let us know that these are the initializer so here's our class human remember this is a class not a struct you with me so far so let's create it so save our human a and we'll say Cameron say 29 and wait 160 there's Q&A let's save our human B and we're gonna say equals human a this is what we did before right so we're taking human a then we're creating a new variable called human B and putting human a inside of that so let's go ahead and print human a name and let's print human B dot name so Cameron and Cameron which is how it we started off with the struct okay here's where things kind of get interesting so now we can say human B dot name equals Joe Joe okay now what I'm going to do is just copy these again and print what would you expect to happen hmm wait a minute wait a minute why is human a saying Joe Joe we only assigned it to human B that is because we're working with a class and a class is a reference type it's actually holding a pointer in to a memory address you know you may have seen stuff like this you know Neha's 0x you know whatever you may see something like that you know those are actual like memory addresses like on the memory chip inside your phone okay or on your computer whatever you're building for so it's actually holding a reference to a single location so when you save our human be equals human a you're actually just giving it an alias can you just kind of giving it a new name you know like your name might be Joe and you go to play a sport and they put you on you know the red team and give you an ID you're number 23 on the red team well your name is still Joe so 23 and Joe actually mean the same thing same person so it's kind of the same thing a reference types holding a reference to a particular object and this is where a lot of people get confused because if you pass an object into a function it's actually not making a copy at all so if I say function modify human and you pass in a human of type human okay if I say human age you know equals 10 if I did something like that it would actually be changing the object that I passed in so if I was like modify human if I called that function and passed in human a or human B or whatever it's actually gonna change the object anything you do to this parameter is gonna change it it's gonna change it okay it's not a copy at all whereas if we said the person remember the struct up there we did the person okay in this case here the person's going to be a copy and this is a copied version of the object you're passing in so when you pass in a person it's going to be a copy okay pretty cool okay so this is just a big thing that you should know and understand super important to know and understand one should use one or the other well so one thing you should know is that classes can implement inheritance and whereas strux really can't so if I wanted to say struct let's just say struct humanoid or we just say living creature right we want to have some base features so if I was to say like var you know does it creature have eyes make it a boolean okay you know var let's say heart rate some type of living creature okay that everything should have whether it's a person or dog or whatever normally with a class you might just say oh let's inherit from living creature but on a struct you actually can't do that now we can implement protocols on Struck's but with a class that's where inheritance really comes in handy here so if I say class just say living and living thing just change it up here and I save our heart rate of type double was just in this case I'm just gonna say zero so I don't have to create an initializer here and then save our what else let's say how limbs how many limbs is this living thing have we'll just make this an int you know could be an octopus have eight or human have four okay so now what I can do has no initialized oh right let's just say this to zero and now what I can do here on the class is I could say it inherits from living thing like so okay so now and if you've never used inheritance before what this means is I can do things like say self dot limbs and look it's available I can set it here okay I don't know anything within eight limbs but you know AIDS or something like that whatever alright and we can't we don't want to do that in initializer probably like in some of their function like add limbs if that was you something you could do you could say self dot limbs equals five or whatever okay so notice how we've got inheritance going on here we can inherit from a class whereas we can't do that with a struct but what's the struct you can do similar things so instead of making another thing here called living creature what we could do is we could create a protocol called living creature and then we could put things inside of it you know living creature and then you know maybe we put some stuff in here limbs type I'll just say limbs for instance you know you could making sure this builds you know then we could implement an actual protocol here sorry living creature and then it would make us implement the protocol just set it up come on do it for me do it for me all right and then it's gonna force you to implement it here I'm just going real fast here so I'm not I'm not actually so that I added that one here as well too because I had to add the protocol but anyway I'm gonna undo all that stuff there just to show you that you would use protocols protocols on a struct instead so one should use one or the other well classes are great if you want to use features like inheritance okay if you can use protocols use those so again the the ideal thing to do is to use strux and then use protocols where you can if you're brand new to programming or protocol czar still confusing feel free absolutely to use classes and use inheritance with classes as you might be coming from other languages but protocol oriented programming is a new thing that is a rather a thing that's becoming much more popular with Swift so learning how to use structs and protocols is great whenever you're working with data models you Struck's like so if you need to create data in your app you know a person a human or this or that you know create a struct classes become more useful when things become more complicated when you need to add some more features and things like that but the most important thing I wanted you to get out of today is that Struck's are value types they make copies and classes are reference types they're referring to an object in memory like an address to a single thing so when you assign other things to it or change it it's going to change them all okay so when we create and bar human B it's actually not allocating another object in memory it is over here with the struct but it isn't what the class is just referencing the memory address of the first object you assigned to us so that's really important to understand so that's it for now this is classes versus trucks use structs for data models let's keep it simple here you're in your early days of learning so just use trucks when you want to work with data classes when you want to use inheritance and add more features that are unavailable with trucks and that's it for now
Info
Channel: Devslopes
Views: 4,548
Rating: undefined out of 5
Keywords: learn iOS, learn app development, build apps, flutter, ios12, programming, app development, programmer, learn to code, app tutorial, iOS tutorial, coding tutorial
Id: rmD4y_p343g
Channel Id: undefined
Length: 15min 33sec (933 seconds)
Published: Sat Mar 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.