Swift Structs - Value Type vs. Reference Type

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's beginner video we're talking all about strux we're gonna talk about how they're similar to classes but mostly how they differ and when to use each one and that really boils down to reference types versus value types and that's a very very common interview question so it's a pretty important topic but before we dive into all that I got to thank today's sponsor today's video is sponsored by Devon Mountain deaf mountain is an in-person coding and design bootcamp that offers housing at no extra cost for full time immersive students now if you know my story I myself and they boot camp grad it was part of the process to help launch my iOS developer career which I'm going on year 5 now in this wonderful profession but aside from their iOS development program dev Mountain also offers programs in web development software QA and UX design they even have a career services team to help you with job placement and financing options are available and Deb mountain loves hearing from my viewers so if you or someone you know is ready to start this journey into iOS development be sure to check out the link in the description alright back to the video alright as I mentioned in the intro classes and structs share a lot of similarities so I did a video all about classes I'll link to in the description if you're not familiar with classes definitely go check that out where we're kind of building on top of that concept here to explain Struck's in the difference between the two and speaking of the difference is let's talk about reference types versus value types so right away classes are reference types structs or value types so what does that mean well a reference type points to specific data in memory whereas a value type you know gets copied in a new instance of it gets created all the time and I know that may have sounded confusing let me use a common analogy that I like to use that I think will help clear it up and that is the difference between a Google sheet and an Excel spreadsheet so a reference type which is like a class it's kind of like a Google sheet right if I shared a Google sheet with you I'm going to see your changes and if I make changes you're gonna see that right there's like one source of truth that we can both edit and change so that would be a reference type and we're gonna demonstrate this after I explain this by the way a value type would be like at the Excel spreadsheet right where I can create a great Excel spreadsheet I can email you a copy and now you have a copy of it whatever you do and edit to your Excel spreadsheet doesn't affect my spreadsheet right there's a copy that you can use in be late however you like that would be a value type like the Struck's so hopefully that gives you a basic idea of what's going on with reference type versus value types now let's demonstrate it in some code here like I mentioned we're building on top of that class video so check that out if you're not familiar we have our developer a class here and let's talk about that reference type I just mentioned right so here I have a development object named Sean which I'm initializing a developer and I'm giving it the name Sean the job title iOS engineer years experience five right these are the properties that are attached to my developer class and this is where the similarities between structs and class are you're creating these objects that have properties on them they can also have functions etc and if you're not familiar this is the initializer for my class more on that in a second when we get to structs constructs get a special initializer we'll talk about that but to catch you up here we're talking about class right now and I have a instance of Sean which is a class developer right so what I can do if I create var Jo equals Sean now I just created a new variable called Jo and I made it equal to Sean so right now the variable Sean on line 16 is pointing to this developer I created this data that same spot in memory and I also create another variable called Jo that's pointing to that exact same data remember I said a reference type points to the same area it's kind of like one Google sheet that two things can change so watch what happens when I do Jo name equals and I change that name to Jo all right if we run our playground I'll move my head here and if I click on this developer you can see the name here the name is Jo so now watch what happens when I just do Sean dot name now run it in here you can see Sean dot name equals Jo because again the variable on line 18 is variable in line 16 were both pointing to the same object in memory so when Jo changed the name the name of Sean also changed again it's like a Google sheet so again that's what happens when you have a class and that is a reference type now let's move on to structs and really all we have to do to change this to a struct up here on line three let's type struct make it a struct now strokes have what's called a member wise initializer like for a class you have to have this in it but for a struct you have kind of a default you know member wise and Eliezer and what it does it just kind of infers it like hey I know I should have a name job title years experience and actually I don't need these to be optional that was just left over from the class video apologies for the confusion but let me prove this to you right we don't need that in it for a struct so let me actually get rid of this well let me comment this out first because I don't want Xcode yelling at me and then I'm gonna delete this Shawn variable here we're gonna create it again but just to show you what the member wise initializer is we get that by default it's pretty smart right so I can do var Shawn equals developer and then if I get the initializer here you see I get the completed initializer right there and then we can do Shawn job title iOS engineer and yours experience five so like I said that was just when you do a class you have to have the knit with the struct you don't so let's come back down to our little exercise here well uncomment that and then I'm gonna run it again we're gonna walk through it right because remember what I did first was I created a new variable called Joe and I made it equal to Shawn well when it was a class and it was a reference type that also changed Shawn but because now we're a struct where I value type so a copy of Shawn gets created and gets assigned to Joe so when I do Joe dot name equals Joe cool as you can see here again the name is Joe so I changed the Joe variable from line twelve that name is Joe but now you can see below should probably already saw Shawn dot name is still Shawn because again this is just like me emailing the spreadsheet right Joe is just a copy of Shawn it's not the original Shawn so hopefully that explained the difference between reference type and value type like I said that is almost certainly gonna be asked on a job interview for a junior iOS developer you may be wondering now like okay I get to similarities I get the reference type versus value type but like you know when would I use a struct versus a class like what's a good kind of rule of thumb a main benefit of a struct and this is will also help you on your interview question is that they are lightweight and performant and the reason being is because structs don't have inheritance right so classes you can subclass it right you know parent classes subclasses for example UI button is a subclass of UI view you know it's a lot of sub classing going on in UI kit that's another benefit of Swift ey s with UI deals with a lot of Vuze trucks and those can get copied and destroyed and recreated very quickly very performant and swith UI relies on that whereas UI cant relies on a lot of inheritance and sub classing so that's a major difference here but if you need to subclass developers say I wanted to say I did this general class called developer but I wanted to create a subclass that had its own behaviors called iOS developer well if I want to do some sub classing I need to be a class you can't subclass a struct however if you don't have a need for that inheritance that sub classing that's a good rule of thumb to go with a struct there so again to wrap that up classes and structs are similar and that it's a way to create objects with you know properties functions etc but the main difference is reference type vs. value type remember reference types which is a class is like that Google sheet where there's one source of truth and a couple different things can change it whereas a struct is a value type that gets copied every time it's used and you know it's like the Excel spreadsheet getting emailed that you can change your own copy and nothing happens so hopefully now you have a better understanding of structs the difference between Struck's and classes value types reference types hope you enjoyed the video see you in the next one
Info
Channel: Sean Allen
Views: 9,599
Rating: 4.9708028 out of 5
Keywords: Swift, Swift Tutorial, iOS Developer, iOS Development, Swift Code Tutorial, Swift app tutorial, swift struct, swift class, swift value types, swift reference types, swift value type vs reference type, swift reference type vs value type, swift struct vs class, swift struct tutorial, swift struct example, swift struct class, struct in swift, struct and class in swift
Id: 3ox38NBBiNM
Channel Id: undefined
Length: 7min 53sec (473 seconds)
Published: Tue Jun 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.