Struct vs. Class in Swift 5 (Xcode 11, 2020) - iOS Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] what is going on guys welcome back to another Swift video in today's video we're gonna be talking about a topic which is super common in interviews it's something that a lot of folks often struggle with and that is a struct versus a class so this is something like I mentioned it comes up and interviews quite a bit and it's not too difficult of a discernment between the two but let's do some examples we'll talk about some real-world usage and all that good stuff don't forget to smash that like button before we get started hit subscribe while you're at it and let's jump into it by creating a playground so open up Xcode like we have here and we will get started with a playground we're gonna stick with a blank playground and I'm gonna go ahead and call this struct vs. class go ahead and save it and hopefully execute decides to load there it goes perfect and let me expand this we don't need the simulator let me expand the window a little bit to give ourselves some more room to work and let's get rid of this string let me actually bump the font size a little bit for everybody and let's get into it so I'm sure you've seen as you've been kind of learning iOS and Swift that you can represent objects in either a struct or a class so let's say you have a person and you created as a class a person might have a first name property as a string similarly a last name as a string maybe an age I will stick with that now we can also copy and paste this right below and change the keyword to be a struct and the question arises of what is the difference well the first difference is if we give this a second while let's first change the name so it doesn't complain that their name the same the first difference is the class we'll give you an error that it doesn't have an initializer whereas the struct does not so this is one of the kind of nitty-gritty details so what this is basically complaining about is that because these are all constant properties that needs to be initialized you need to give it in initializer and it should ideally take in all of those properties and basically you can now construct this class and assign them so we're gonna say first name equals first-name lastname equals lastname equals last name and age equals age so that actually isn't the core difference that's just one of the little nuances the core difference about classes versus trucks is passing by reference type and value type so this is where it gets into a bit of a computer science II topic Struck's are passed by value where classes are passed by reference so what does that mean so let's do an example to take a look at it so let's say we create a person I will pull out a struct I guess we can't do that let's call it underscore struct so it doesn't complain we'll say this is a person struct so we're gonna assign this I can spell today we're gonna assign this to a person struct you can open up the constructor and you can notice that it gives us what we want we don't need to put the initializer here so we're just gonna say ab12 and then let's make this 14 this one is just gonna be person which will be the class just gonna underscore a class so we know the difference and we can keep the same stuff so what do you guys think happens when you create another person and say this equals class what's actually going on is this property that you have created is now equal not to the value of class but a reference of class and memory so when you go ahead and you update class that first name to something else and we would need to change first name to be a var on here make it mutable that actually goes ahead and updates it on the reference of the original class however if you go ahead and change this another person - a copy of a struct rather if you assign it to a struct what it's actually doing I kind of alluded to it already is it's copying the value of it and creating another struct so if you come in here now and change the first name again this time for the struct and we're gonna again make this a var so it's mutable the original value of struct up first name will stay as a but the another person that first name will be ABC so the core different difference to really pick up on here the takeaway is at interview questions a struct is passed by value it class is passed by reference so that kind of brings us to the question of why do we care which one is better when to use which one right so when to use one or the other is a bit of a subjective argument and it kind of always has been generally the rule of thumb with most iOS dev is if you have something that is really dependent on the value of the data you probably want to use a value type aka a struct so an example of that would be a model or a view model so if you're familiar with MVC or mvvm any of the coding patterns if you have any data that is driving a view like a view model or some presentation then let's say we're building the Facebook app and we have a Facebook post a view model and let's say for hypothetical purposes there is a like account on here and we'll make it a var so it's mutable and let's say we construct it and we pass in 12 every time that the model changes we want to trigger some type of view update presumably right so this is how on the feed you can actually go ahead and see those live updates this would be an appropriate usage of a view model now this is not to say let's see oh this is complaining let's say there should be a var this is not to say you can't use a class for your model however a class would be a bit of a heavy-handed usage of a class right there's no really benefit for us having this be a reference type if anything it gives us kind of issues because if we pass a and try to copy it copy a reference an instantiated reference of it right instantiate an instance I should say not really a reference it becomes problematic because we're not actually pointing to a copy of it we're pointing to the original reference so next takeaway view models and models superb uses for trucks and trucks and just data types value based models that really don't need the reference type bit of it let's see what else so the other thing that you should know about structs and classes in terms of what makes them similar and different is both of them can conform to protocols so here we can have this conform to hashable as well as this class conform to you hashable so pretty simple in that regard they basically do the same thing this is gonna start complaining because we don't actually conform to it so we need to actually add this here and I'm just gonna return true we're not going to implement this I've got a separate video in hashable if you want to learn more about that the other thing which is similar between both of these types is that they can both have functions and they are truly full-blown functions and you can basically throw them in both a class or a struct so in a lot of other languages classes and structs have inherent limitations where structs are a lot more dumbed down and Swift not really not really the case also both of both structs and classes can have computed properties so let's say we have is ready which is gonna return a bool we can say we can do some if condition in here and based on that we can return something we'll just return true for now we can also put this in a struct and return false and that is also completely valid and good to go so other than that that's about it personally my preference if anyone cares is I like to use Struck's where I can one part of the reason is I don't have to put an initializer and lazy me it likes to cut that corner it's not incorrect if anything it makes your code simpler I'm sure you've also seen in a lot of frameworks Apple themselves really pushes using strux and if you're even more curious about the way to use internally at Apple Swift UI is heavily built on structs so in Swift ey for those of you who are familiar with it every single view that you can create in a very simple way is nothing more than a very generic struct and they call it view and that's why apple with Swift ey severely encourages creating as many views as you want whereas traditionally all of us iOS Deb's really kind of make a face at having huge view hierarchies because it just becomes difficult to maintain but Apple is turning around and saying hey don't worry about it structs are super lightweight go ahead and create as many duplicate views as you want we'll make sure we can handle everything for you and the reason they can do that in such an elegant way is that they're all value types and backed by a struct so that's where I'll wrap up the video pretty straightforward concept yep pretty important definitely an interview question at even senior level they might throw this at juice in some way shape or form so definitely make sure you understand this if you haven't destroyed that like button already make sure to do some of the YouTube algorithm hit subscribe while you're at it if you've got a question or anything throw it down in the comments happy to hear from you guys as always thanks for watching and I will catch you in the next one
Info
Channel: iOS Academy
Views: 5,300
Rating: undefined out of 5
Keywords: swift 5 for beginners, swift, swift 5, swift tutorial, swift value types, swift reference types, swift struct vs class, what is the difference between struct and class, swift class, swift struct, swift ios development, ios, app ios, swift make app, swift programming, swift development, swift app making, how to make iphone app, swift basics, swift fundamentals, swift tableview, swift custom cells, swift 5 wwdc, swift 2020, swift class 2020, swiftui, swift ios 14, swift ios
Id: KCYxB-q3pxk
Channel Id: undefined
Length: 10min 19sec (619 seconds)
Published: Mon Jul 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.