Hashable & Equatable in Swift 5 (Xcode 11, 2020) - iOS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] what is going on guys welcome back to another Swift tutorial today we're gonna be taking a look at the hashable protocol and respectively the equatable protocol so here we are on the apple developer website just looked up hashable and you can see the definition is a type that can be hashed into a hash sure to produce an integer hash value now I don't know about you guys but that's pretty meaningless to me unless I see an example hashable super popular used all over the place so I figured it's probably about time that we started talking about this so that's it make sure you destroy that like button down below from the YouTube algorithm as always really destroy it let's see if Google has to go back and fix that button hit subscribe while you're at it while you're down there in the button section get Xcode ready get excited let's jump right in quick pause before we get into the video if you haven't seen it already I am hard at work putting together iOS Academy io a community where all of us iOS engineers can come together learn how to build some of the top apps like Facebook YouTube and Instagram in addition to interview prep to learn some of these iOS roles at top tech companies so if you're interested in the free and premium content to come head on over to iOS Academy IO and enter your email address into the waitlist form and you will be notified as content becomes available that said let's get into the video all right so we're gonna get started by opening up Xcode and creating a playground here we can stick with the blank templates and we can go ahead and leave the name as the default as well it'll just overwrite the previous one that I went ahead and created earlier today let me go ahead and move this window to be centered and let's expand this guy to give ourselves a little more room to work like so so cool first things first let's get rid of the string and we're gonna put our own string here we'll say string 1 let's make this let's say string two and make this hello as well so now obviously something you can do is if string 1 string 1 equals string 2 we can like print something out have you ever taken some time to step back and wonder how does this work right and a lot of these programming languages we compare things all the time if something is equal if something is not equal how on earth does a compiler know if this equals this right so a swift is doing actually under the hood is it's leveraging the equate able protocol and we're gonna learn about that that's gonna be the first thing we touched on equatable and then we'll dive into how it is related to hashable and how those two things really work hand-in-hand so this the standard library has equatable tacked on to most of the standard types now obviously the circular string so they support equate ability and that equate ability allows the compiler to know that in what sense rather what criteria is it using to compare of this equals this right one thing is number of characters one thing is the characters itself now this case case-sensitive so now this does not equal so sometimes we'll see people lower chasing stuff so on and so forth so let's say we created our own class and we wanted to check if it was equal right so let's say we created a class and we're gonna rather use a struct now let's say we've instructor that's called car let's say it takes a make and a model and let's say we create a BMW which will be a car make is obviously a BMW and the model will be a 5 Series and let's say we create a on a Mercedes so a Benz we're gonna say V make just get lazy and do an Benz and the model let's say this is a II class now let's say we want to check if these two things are equivalent right if we do if V M W equals Benz and same else print different let me fix that else in a second you're gonna see we're gonna get an error in that quick second here so if I try to run the console rather run the playground you'll see the first thing that we see here is binary operator equally will cannot be applied to two 2-car operands this is a fancy way that it's basically complaining and saying hey I don't know how to compare this thing in this thing and they're both of type car help me figure out how to compare it so in this case it's pretty easy all we can do or need to do is add the equate able protocol to our struct and you see that this error goes away now and if we run it we'll see that they're different obviously right now let's go ahead and change these to be the same and let's run it one more time and it should say they're the same okay so what's going on here how how come when we add equatable it magically starts to work well the first thing you need to understand is the compiler is smart enough to know that this car struct is simply wrapping two string types so what is essentially doing is once you make this car equatable it looks inside of the struct and it says okay well I know how to compare strings so I'm gonna say if both of the strings in a car or equivalent to both of the strings in another car they'll be equal right so like if we make this BMW we leave eClass you'll see that it should say that they're different because they indeed are different right both strings don't match up so this is a pretty simple example where the compiler and standard library is doing stuff for us right now what if we add another type on here and we call this type owner rather another property on here and it's gonna be a custom property we're gonna make we're gonna create a struct called person and they're gonna have a name now person is not equate Abel you'll see an error pop up here in a quick minute there it goes the first thing is complaining about is we need to pass in a owner so let's do that here and we'll say Joe owns the Beemer and who owns a Mercedes he'll let I own the Mercedes sounds about right so those errors are gone but you see we have an error here and it's saying that car does not conform to the protocol equatable so similar to any protocol when you get this error you can hit this and hit fix and it should stub out basically what it needs and if you look at this this is a weird-looking function the function name is equal equal where are we seen that before so this is actually what brings me back to the very first thing that I said when you have two strings and you say the string one equal equals spring to the equal equal operator is nothing more than a function that it's using to make a comparison now in this case we have two parameters in this function two LHS RHS they stand for left-hand side and right-hand side so what it's basically saying is hey I don't know how to figure out how to compare these things on my own can you implement this function and tell me so wherever someone uses equal equal width to car types it's able to do it so we're gonna implement this so we're gonna say return if the left-hand side dot make equals whoops we will make to start off the right-hand side dot make and left-hand side left and side dot model equals RHS dot model but then we have this person thing right owner but owner does not equate the equatable the simple solution would be just to make this equatable and everything but magically worth but I purposely didn't do that so we can show how how to implement this function well let's say we also want to account for the person we can also say and LHS dot owner dot name equals equals right hand side dot owner dot name and now what you'll see is we don't have any errors about this not conforming to equatable let's put that down there and we can indeed now try to compare these two and let me let me use this name string values in here and our person is different don't mind the various property names being kind of misleading now they're both beamers but go ahead and hit run and you'll see that they're still different right and they should be because in our equal equal implementation here for equate Abel we're taking into account the name of the owner and it takes into the account of that name in a case sensitive way so let's say I make this one the same name but it's case sensitive one has an uppercase and one doesn't I'm not mistaken it'll still say it's still different because it is now what if we say we lowercase both of those strings for the name and we try this one more time you'll see this you should see that they are now the same so you see they're now the same so the example that I want to kind of drill home is equate Abel is kind of the magic glue that's doing this equal equal for you and in the cases where equatable can't handle it for you you are more than welcome to conform to it directly and you're encouraged to if you need to be comparing two objects that are complex objects now you can imagine obviously this is pretty common so what's in the Facebook app Facebook wants to compare of two posts are the same right opposed let's say that was sent in to be deleted to like some function and a source post they would need to make the post be equatable or they can try to compare the instances things like that but you cleared was pretty darn important is my point so let's do one more thing let's talk about hashable now which is kind of how this video started but both of the things these things are pretty related so let's say we have an app where let's just keep it the Facebook example let's say we have a struct that represents a user account and let's see we have a struct that represents a post and let's just have text on here let's say the user account has a account identifier I'll call it a string and it has a collection of posts right in a rate of post objects now what structure that would be nice to hold let's say multiple people and post would be a dictionary right so like a dictionary a quick refresher let's say we have a dictionary and the keys are strings and the value is a string as well this is a pretty standard dictionary in the case that we want we want the key to be the account and we wanted to pull to point to a collection of posts so let me actually get rid of this property so what we really want to achieve is we want something like this we want the key to be a letter that call a user account right we want the key to be a user account type and we want this to point to a collection of posts so go ahead and do that and see what happens so okay we've got an error so let's see what this is saying it's same generic struct dictionary requires that user account to conform to hashable type user account does not conform to protocol hashable so what the heck is hashable so a dictionary for those of you that are kind of from a CS background or maybe even if you're not a dictionary the unique aspect of it is the reason you can get any value out in an oo of one time complexity space-time complexity is because everything has a hash value so a hash value for the key that is pointing to the value needs to be unique right the the compiler and the language needs to be able to figure out how to differentiate between one user account and another so in other words let's say we had let's say this actually was working and let me actually make this look nicer let's get rid of that it's good that let's put a user accounts in here I will say the first identifier is this and this points to in array of posts I will just throw one post in here for now cuz I'm being lazy and let's just say first the compiler needs to know how to disambiguate and how to figure out which user account is unique right like if I say from this dictionary give me all the posts for this user right it needs to know how to figure out well how do I get a unique hash to get to this position in the dictionary and that's where hashable comes in and it sounds a lot more complicated than it is but all we need to really do is conform up here to hashable whoops that's not what we want to hashable and once we conform to hashable this'll start going at us give it two seconds well actually it won't so similar to equate able hash trouble basically uses the types that are wrapped inside of whatever you're making hashable and it knows how to get a unique hash value for a string so it's able to do that in this case and it doesn't complain and similar to equate able if we create another struct let's say we make a person once more let's see this is a user account let's make it let's make a person why not I'll stick with the same example and let's say we put a name in here now if this one is not hashable you'll see that it'll start complaining in a second actually if we make this guy person as well it'll start complaining because this thing is not hashable or equitable give it a second there it goes so it starts complaining that you're not conforming to hashable here and if we hit this error and click this fix you'll see that it actually put in this equal equal that's weird this is an equate able thing so the point of me talking that both equatable and hashable in one video is they go hand in hand if if the language can figure out if two things are equal it can figure out how to kind of do the opposite right if one thing is unique so similarly there's an inverse function to this which it's not not putting in for us and that is hash into hash sure now what pac is us we can basically use this to get a we can also bring any credible actually we can use this why don't it just stub it out twice for us okay let's move this up here we can use this to basically create a unique hash value from the types that are on this class we can say hasher combined what do we call it account identifier dot name and I think we can get away with getting rid of it equal let's see let's see if you complain to us so yeah I guess we do need it so this will basically generate a hash integer a unique and if you don't know what a hash is I keep saying it's basically just a unique integer representation of some type and the equatable thing we do the same thing that we did in the beginning of this video we say left hand side we called the account identifier dot name equals right hand side count identifier dot name and now we're able to let's see it give this a second can I convert value because this should be a person now let's make this guy whoops it's loves to pop up in all my videos let's make this one a person as well you'll see that the errors should now go away soon when I typed everything correctly user account it's just a person I think you have an extra parentheses there now this won't complain and in this case it wants us to also pass in a person let's pass the one of the ones up top there let's say we want let's make this a letter so it's consistent let's say we want a and we're going to print out in a second so this should return a collection of posts so if I print out posts and I spell it correctly we should see the second posts print it out here so let me expand this and go ahead and pause this clear and hit run and what we should see is it's an optional post we get first out so let's see what we expected so we have first in here and this one looks for a and we were trying to get the thing for a which makes sense the reason it's optional is because we're trying to randomly access something from a dictionary so it may or may not exist right it's on an array so we could do with something along the lines of I'm going something like that it won't be diction it won't be a optional anymore now let's go ahead and change this to BB and we should get the second thing out so let me go ahead and pause this clear with the command K go ahead and hit run and now we get the second post out so does quite a bit longer video that I anticipated this to be but sometimes hashable and equatable get a little convoluted so they're very hand-in-hand they basically are the underpinnings of how do you compare two things and Apple uses them themselves actually quite a bit in their own code and standard library so pretty common you'll see across code bases pretty useful if you try to put something in the dictionary as a key before it got that error that we saw earlier now you know why so that's it I'm gonna wrap this video up here if you haven't smash that like button already make sure to do so with YouTube's algorithm subscribe if you're new hit that like button again I'm gonna keep saying it and so everyone smashes the like button throw a comment down below if you have any questions thanks for watching I'll catch you the next one
Info
Channel: iOS Academy
Views: 4,828
Rating: undefined out of 5
Keywords: swift 5 Hashable, swift equatable, hashable swift 2020, swift 5 tutorial, swift tutorial, swift for beginners, swift 5 for beginners, swift tutorial 2020, swift 5 beginners 2020, swift protocol, swift beginner basics, swift what is hashable, swift dictionary hashable, swift equatable 2020, swift what is equatable, xcode 11 tutorial, what is ios 14, how to make ios app, swift ios app, swift make app, swiftui, swiftui beginner, wwdc 2020, swiftui tutorial 2020, ios, app, table
Id: 9GQZ1aharGg
Channel Id: undefined
Length: 18min 38sec (1118 seconds)
Published: Mon Jul 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.