Adding conformance to Comparable for custom types – Bucket List SwiftUI Tutorial 1/12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] when you think about it we're taking a lot of things for granted when we write swift code for example if i hadn't hear uh funk example and then said let result equals four is less than five would expect result to be set to true four is less than five the developers of swift and nlvm which is the larger compiler project behind swift they've already done all the hard work of checking whether this calculation four less than five is actually true so we don't have to worry about it now what swift does really well is extend functionality into lots of places using protocols and protocols extensions for example we know that 4 is less than 5 is true because we can compare two integers and say yeah this one comes before this one or not or whenever we want to and swift extends that functionality to whole arrays of integers we can compare all the numbers in an array and then say which one should come before all the rest of them as a result we can sort the array and so in swift would expect this kind of code to just work uh let values is one five three six two nine dot sorted and then we can go over that in swifty y and say uh let's have a list of those values id of backslash dot self and then text string of dollar zero and we just assume that's gonna work there we are one two three uh five six nine so it works exactly as expect and we don't have to tell sorted how it should work because it understands how arrays of integers work it can compare individual numbers so we can compare a whole array of numbers now consider a struct like this one there's a struct called user which identifiable has an id uuid plus a first name string and a last name string now we can make an array of those users and use them inside a list we could say uh let users is an array of user uh the first name arnold last name rimmer and then a user with first name christine last name ko chansky and then user first name david last name lister and then to place that into a list we'll just say a list of our users like this with a user coming in and then say show a text view with uh let's say the user first name capital n and then user last name last name like that and when it plays back we're going to see rimmer kaczynski and lister my mistake lister sorry red dwarf fans there we go lister and that's gonna work because with it we've made the user strut conform to identifiable so it'll just work the list directly but how about if we want to sort these users in some kind of order and if we modify this to say actually create my user's array and then sort it that code won't work swift does not understand what sorted means in this context doesn't know if you mean to sort by first name or by last name or by both or by something else or who knows what now previously i showed you how we could pass a custom closure to sorted we could say when we're sorting i want to sort using dollar zero dot last name is less than dollars one dot last name and now we're going to sort last name first so we have kochanski lister and and that absolutely works but it's just not ideal for two main reasons first this is what we call model data by which i mean it's stuff that affects the user struct it belongs this data here and this struct and its properties they're what we call our data model the data behind our program and in a well-developed app we don't really want to tell the model how to sort itself you know our swift ui code this represents the layout of our code not the data and so ideally our data knows what sorting means second what happens if we have users elsewhere in our program we have assortment multiple places you might copy and paste this closure a few times before realizing you're just making a problem for yourself you're having to sort the same way in every single place copying and pasting your code in every single place if you need to change your sort algorithm to have last name and first name for example you've got to find every place sorting took place and then and then fix it so it's problematic swift has a better solution which is that we could have a sorted algorithm built in to our user now when we had our rave integers we could say sorted with no extra work no custom closure being passed in here because swift knows how to compare arrays of integers it's built right in in coding terms this happens because int integers conforms to the comparable protocol which means it defines a function that takes two integers and returns true if the first should be before the second now we can make our custom types also conform to comparable and when we do we'll get that same sorted method that works with no except parameters and this takes two steps step one we want to add the comparable conformance for our user comparable and then step two make a method inside there called less than that takes two users and returns true if the first should come before the second so we'll say static func less than left hand side's a user right hand side of user returns bool and return lhs.lastname is less than rhs.lastname that's not a lot of code right but there is still things to unpack first up yes this method is just called less than right but it's a it's the less than operator that's the name of our function and its job of this function to decide uh whether one object the left hand side should come before the other one the right hand side so we're adding functionality to existing operator here you know you can say four less than five or hello is less than world but now you can say is user one less than user two this is operator overloading just in our custom version here it's both a blessing and a curse trust me uh second i was reading it out but lhs and rhs they're coding convention for left hand side and right hand side because the less than operator has one up around the left and one on the right blah four is less than blah five for example third this thing must return a boolean we must decide whether one object comes before another object in the sorting order there is no room for is the same here that a whole other operator called equals equals which comes from equatable that's not here this is just comparable is it less than or greater than something else and it must be marked static this thing belongs to user struct directly rather than a single instance of that struct internally the logic is pretty simple we just go ahead and pass it on to the string a string a less than string b that's it of course you can add more logic here either the same maybe do a first name comparison or whatever but here last name is more enough so we can do that just fine here but ultimately you must return true or false now what you can't see here is that conforming to comparable gives us access to greater than uh as well we get it automatically because it's the opposite of less than right it's just flips the boolean basically so now we have comparable working here we can go ahead and remove our custom closure for the sorted call we're going to say sorted and that resolves the problems we have before because we now isolate our model functionality neatly inside the struct itself it now understands what sorting looks like by itself and we no longer have to copy that custom closure everywhere we want to sort stuff we can just use sorted everywhere safely knowledge if we ever change the algorithm that everywhere in our code will also change
Info
Channel: Paul Hudson
Views: 326
Rating: undefined out of 5
Keywords: xcode, swift, swiftui, ui, ios, programming, tutorial, example, project, code, uikit
Id: SPML9uatLf8
Channel Id: undefined
Length: 9min 34sec (574 seconds)
Published: Tue Dec 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.