Mastering Generics: Is This The Most Advanced Swift Code You've Seen?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey what is going on everybody welcome back to another exciting episode indeed lots of bill that app channel really hope you're having a fantastic day now in today's video we are going to talk a little bit about generics and how we can use the power of generics to sort of reduce the amount of tedious and also repetitive code inside of our application and so before I dive a little bit deeper into generics I want to show you a quick demo over here of some of the screens inside of our application that is very tedious to build out and it also contains a lot of repetitive code so here we go we have the Instagram of course application and this is just the very standard home feed which contains a collection view controller and we have a bunch of rows inside of here that rendered out each of the posts objects inside of our app right so this is just a very simple list screen and you'll notice that if I click into the comments screen over here the behavior is very very similar and what I mean is we have a list and we have a bunch of cells and then each of those cells are responsible for rendering out an object in this case it is just a very simple comment object alright so in today's lesson I would like to kind of show you how we can use generics to build out each of these very very simple lists without typing out too much code and the way I'm going to illustrate how that works is to give you a quick example right off the bat here so inside of this brand new single view application project I have a app delegate file which kind of looks like this right now and inside of this file I have a bunch of code that I have commented out so let me uncomment it and let me kind of show you what's going on here at the very top here I have the model or the view mano class which is just a person object and that's just a simple struct with first last name and right below I have this view class which is our person's cell and this guy is some kind of generic cell which we'll get into a little bit later this person's cell has a 1 property on it called item which is a person object you can kind of tell right off the bat here that these two classes are sort of related right and down below all the way on line 43 we have this controller class called people controller and the responsibility of this controller is actually list out a couple of people in a vertical lists so items is person though Clinton and Barack Obama okay and this guy is just some kind of generic table view controller and that's all we have so far and typically when you're setting up your iOS apps you kind of go through this pattern of MVC or mvvm now with all of those classes being kind of defined like that I'm going to go up to the top for our app delegate and I'm going to create a window make it visible and for the root view controller we're going to use a UI navigation controller and the root is just going to be an instance of this people controller class over there and so now that you can kind of see a little bit of the code I'm going to run it now inside of the simulator to show you what the application renders out and so basically using just a couple of lines of code here here and there we aren't able to render out a very simple vertical list of the people inside of this items array and so typically when you're trying to construct a list you have to go through the the boilerplate code of number of rows registering your cell dequeuing your cell and then setting your view object on that cell and so we don't have to do any of that code and all you got to do is you want to set up your items array and your view class and your model class and everything is going to be done for you now if I wanted to add a third person it's very simple I just append to this array and just create another person object like that and you know maybe we want to use the Donald Trump person and just run my code again and you'll see the third cell will now be populated with Donald Trump once the application is up and running and there we go okay so this right here is quite a of code that we just went over and you probably have a couple of different questions right off the bat here and the first question is what exactly is this syntax right over there so this is called a generic and inside of here is our person generic and down below we have very similar syntax with two generics so the first generic is person cell and the second generic is a person and then the other question that you might be having is what exactly is this generic table of you controller and this generic cell well it's nothing really that special if you command click into this it'll bring us into this file and at the very top this is just a generic table view controller and it's a subclass of UI tableview controller and it does a bunch of stuff here and we have generic cell all the way down at the bottom alright so let me kind of just navigate back to the app delegate and let me tell you this right now if we try to kind of dissect exactly what the code is doing by just looking at these three classes it's pretty hard to kind of figure out what's going on so what I'm going to do in today's video is to show you how to use generics to construct a very simple generic or base table view controller class so that you can sort of create this behavior right here now one thing I do want to say here before we begin coding is that I think generics are probably one of the most advanced topics of iOS development but it turns out is actually not that difficult if you have a good explanation as to what's it going on alright so having said that I'm gonna try to do my best to walk you through all this stuff let's go and get started with Xcode right now alright guys welcome back to the coding session of today's video the way we're gonna start off here is to take a look at this sample table view controller class and let's kind of see what it is doing it's not doing very much but let's just take a look at it nonetheless instead of beauty and load we have a register uitableviewcell that self we're using the cell ID up here instead of number of rows we're returning 100 cells and then instead of the DQ process we are setting up the text label text using index path Rome okay so nothing too special and what I would like to do is to copy this code over there go back to app delegate and just paste that in there just so that I can run my app right now and I'll show you exactly what the sample tableview controller does so again nothing too fancy we're just returning in 99 cells over there or a hundred cells to be more accurate okay so now what I want to do is to present you a quick question and so basically the question is what happens if you want to create a second controller right well you typically copy all this code and you paste it in here and you know change the class name modify this to maybe something else and you can you know run this code again instead of app delegate using that run that and you'll see your cells look a little bit different it's gonna have two two two at the very end there so just like that and so you can just repeat this question and you can ask well if I need a third controller what do I do well you would go ahead and copy this and paste that in there change the name and change this as well okay so hopefully you guys are able to see where I'm trying to go with this and basically you don't really want to have all of this kind of repetitive code inside of your app if you don't have to right so what I'm going to do is to just replace or remove all that code change that back to the sample so copy that and paste that in there don't run it again and we'll get back to where we were just a few minutes ago so zero one two all the way down to you I believe at ninety nine okay so pretty awesome and what I would like to do now is to kind of show you how to transform this guy into a better class so that we can use it for our application so what I mean is I actually want to change this to something like base table view controller and this guy will be my kind of superclass for all the lists inside of my application alright so what I'm going to do at the very bottom is I'm going is a class and sub the list controller you know call this whatever you want and I'll subclass base table view controller which is this over there so this guy I can actually register different cells instead of the viewdidload method if I wanted to do that so if you didn't load you know table view registers cell and you know call this method to do whatever you have to but I actually don't want to do this because it's already done inside of the superclass and so what if there was an easier way for me to eliminate the necessity of having to register a cell well you can actually do that by introducing a generic up here so let me just type it out and it's a little bit easier to understand once you see the code so let me use a generic T and this guy is going to have to be a subclass of uitableviewcell and so you have some arrows down there but I want to use the T for the registration process and so down here we just replace it there and let me just hit this fix over here at the fix and over here it says uitableviewcell and you can build your project and everything should be fine so let me copy that some list controller and head back over to app delegate there's an error over here which I'll fix by using some list controller instead and now I'm just going to run my application and you'll see exactly the same thing hopefully alright so that looks pretty good and let's go back to your sample table view controller and let me show you what the point of doing this was and the first thing I'll do is to create another class so let me just create another cell class and this guy I'll just call it yellow cell and you know subclass tableview to sell and I'm going to overwrite layout subviews and you know super it layout subviews let's get that guy out of there and set the background color to yellow like that alright so now I can actually use this yellow cell inside of my son list controller so just copy that and if you wanted to use a different cell you can just create one very easily now so you can see all my cells are yellow if you create you know another cell or in a cell you can just change this to orange and just copy this and paste that in there and then now you'll get some more orange cells instead of yellow cells which is pretty nice right you don't really have to go through the registration process over and over again the generic that we introduced all the way up here pretty much handles the registration for you all right so that's pretty nice and the second thing we would like to kind of remove from this sunless controller is the necessity of defining the number of rows section in or this method right here so let's say we want to return a raw number of five and have the list render five cells so what you can do is you know return to five over there and that's kind of what you get and so this is another tedious bit of the code that I would like to eliminate so let me just remove that and what I'll do is I'm going to go back up to base table view controller here and I would like to introduce a variable called items and let it just declare it as any array like so so this is the syntax for an empty array of any objects alright so hopefully you understand what that is and instead of number of rows in section I'll just return items that count so running this obviously well maybe not too obvious but you'll get 0 rows inside of your lists so that's what you get and the reason is because items right now has nothing in it and so if you go back down to sunless controller you can introduce some items and let's just call this I don't know nine eight seven and run your code again so let's fix that over there with a comment and running this hopefully you'll see three cells inside of your controller and so yeah we get three cells right so zero one two and you can see that we didn't really have to override this number of rows method all we had to do was to provide some kind of items array which you typically have anyways inside of your table view controller or subclasses all right so the next thing I would like to do is to somehow set some kind of item inside of my cells here so I'm going to remove the yellow cell because I'm using the orange cell and so what I mean is every time you have a cell object right you pretty much want to pass some kind of model object or a view model object to your cell so typically what you'll do is you'll have a variable on your cell and then this variable is some kind of object so I'm just going to call it item and set it to be type any alright so the next thing I'm going to do is to just make it a automatically unwrapped variable with this bang and then I'll say did set and inside the did set I will also say text label dot x equals this item object now because item is any type right now you have to cast this into a string so that's what I'll do and okay so this is my orange cell right now and what I can do is I can actually modify some of the code in my base table view controller to actually somehow pass the objects and items to my orange cell somehow so let me try to show you how to do this by casting the cell over here to the orange cell as question mark or in sound as bang or in cell and the moment that you do that this cell over here is of type or in a cell which means that it has this item property on it which is of type any so this is the property we just declared and so in here you can say something like you know something and just build your project let me remove that bit of code and just run your code one more time and so what you'll see is that each one of your orange cells will have this something in it like that and so we don't really want to use something right but instead and we can say self dot items index path dot Rho so Rho and let's run our code one more time so basically what's going to show up in our table view list is we're going to see an empty thing like that and so why is it doing that well the reason is because this items array is declaring these three integers and well these integers aren't exactly string objects so let me modify it a little bit here say let's just say hello and let me spell that correctly world and awesome so hopefully those three strings will show up in my list controller and I'm pretty sure it will so we have hello world and awesome all right so let's kind of step back a little bit to see what we have here and basically what we have is some list controller and it doesn't have a whole lot of code inside of it all it's doing is setting up the items array and that automatically determines how many cells we have inside of our table view and it also kind of passes these objects to each one of the cells inside of our table view as well all right so that's pretty good and why don't I remove the orange color code because we don't really need it so we're running it one more time we'll just get the text inside of the cells and that's pretty good so hello world and awesome all right so with everything is set up the way it is right now everything seems to be working okay however there is one problem with this orange cell and what kind of saw earlier and basically that problem is that this object type over here has to kind of match whatever you put inside of this items array and so that isn't really ideal so let me show you how to fix that but first let me rename this class because it's not really an orange cell anymore so let's refactor this rename that and I'll call this base cell instead all right so if a cell is here here and also over here and the way I'm going to actually fix this object type right there is to actually introduce a second generic for my base cell here so let me show you how to do that by using bracket and I'm going to call this generic you over here and let me see let me just type out some more code that way I can explain what I'm trying to do so with this generic you I'm going to make this item over here be of type you it's a bar items and be of type you like that use a bang and that's kind of all I want to do for now I don't really want to use the did set and so the question is that what exactly am I going to do with the base cell of generic type u well let me show you what I am going to do right now and you see I get a ton of errors so I want to show you what the fix is by going all the way up to the top of this file base table view controller and I'm going to make T subclass base cell instead so just paste that in there and if you try to build you'll notice that you get a lot more errors up here as well so clicking on that it says that this base cell doesn't really have a type so how do we exactly fix this right because the base cell needs to have some kind of generic over here of type of you and so what I'm going to do now is going to be a little bit confusing but I want to type it out first so for this base table view controller I'm going to actually declare a second generic as well so this guy can be Z it can be X it can be V or anything you want but to be pretty clear as to what it's going to do I'm going to call it u instead so that's my second generic and it doesn't actually match with this thing over here so they're really different things so don't get confused about that this is just the second generic for my base table view controller and so with that you I'm going to make my base cell also use that generic type of U over here so these two use actually match alright so what exactly am I going to do with that U is I'm going to make this items array also be of type u so instead of any we are going to have a array of type u objects all right so I'm going to try to build this one more time and we're still going to have a problem down here with this base cell if you click on this and hit the fix it'll introduce this anything over here but instead of any we can also use the u generic so let me just paste that in there and what this in line of code is doing now it's it's basically D queuing a cell we're casting it into a base cell and the base of the generic for our base cell is type u which matches this U and this you appeared and also the U for the items rig alright so what that really allows us to do is like this cell over here if you comment out that line because they sell that item the item on this cell this base cell is of type and all the objects inside of items array so this items right here is also type of you which means that we can just get the object out of it using the row on your index path so these two lines are equivalent I can actually get rid of that right there and that's pretty good so if you try to build you are still going to have some problems down here all right let me just first remove these couple of lines of code and now what I'm going to do is I'm going to try to fix this over there so the base table view controller really requires two generic types now so the reason for that is because we have the generic type tea and also the generic type of you up above so this code doesn't really make any sense anymore and the easiest way to show you what the fix is is to actually implement some type of objects up here and let me show you what I want to do and so I'm going to introduce a new type of struct and this guy is going to be kind of arbitrary I wanted to create a dog structure over here and let's say dog is going to have a property of name like that right and let me first create another cell down here before I fix this problem and this guy will be a class and I'll use a dog cell and this guy will be sub classing base cell like so and the thing about this sub class is that I actually need to provide a generic for this base cell because base cell requires a generic type so if you click that hit fix you get the any over here but instead of using just any I could actually use a generic type dog like that so this dog is going to fill in the u object which means that this time is going to be dog for this dog cell so if you type out item you can override it I just say dog like that and it once is a dead set and then for this dog cell you can actually say you know text label that x equals dog right there not dog but I'm going to use items the item has a property of type AB dot name because it is of type dog alright so definitely some of this code might be a little bit confusing if you're not used to using generics but that's kind of how it works and now that I have this bit of code up here let me show you what I want to do to fix this problem alright so again base table view controller is going to require two types of generics or two types of classes in here one for the first one which is going to be our cell so this is the first generic T is going to have to subclass a base cell and then the second Jenner is U which is going to be the generic type for our base cell so that might sound a little bit confusing as well and the answer here is to use your dog cell class like that and for the second unit we are going to use the dog over there and so once you do that you can build your project and you'll get this error down here and so let's see items is going to be of type u and this is probably incorrect it's supposed to be of type dog so what I can do is declare a brand new array and you can just declare some dog objects inside of here let's call this woof and we'll for the name and the second dog we will call let's see is a good name we will just call this rough and rough and rough all right so now this sub list controller has this items of range instead of that array we have two dog objects with these two names and everything compiles okay the last thing I would like to do is probably run my project now to see what the application actually renders out as and as you can see we have woof woof and ruff ruff ruff okay so that's kind of how we fix our application so that we have a table view controller called a base table view controller and we can just subclass it and then also provide a cell and some kind of struct or model object like our dog and then we provide items over here and everything just works out application renders out perfectly like that all right so one thing that you cannot do now and it's actually good because we've kind of declared some rules for our base table view controller right so let's say you have some kind of other cell and this cell will be a type I don't know string cell and that's the name and it'll subclass base cell all right so if I try to use an int over here you'll notice that the item or the item object is of type int like event and let's just say it did set the type that out correctly this text label dot text equals some kind of object string and instead of here I'll just use string interpolation and fill out the item inside of there now the problem with this is that you are going to encounter a compiler error if you try to create a cell with your wrong type of generic object so let's say this dummy controller there let it be a type based table view controller right so the first generic again is going to be a cell so if I use a string cell over here and if I use a string for the actual second generic type it's not going to allow me to do this so if I click over here you'll see that this is a string and it says that base cell inherits or the string cell needs to inherit from base cells string like that so the problem is that this guy over here needs to be a string the type and then if you try to build now your application shouldn't work but you need to change this to be a string instead all right so build one more time and now that all of your types match your application is no longer going to complain about any type of generic error all right so as I kind of promised in the beginning of today's video you're probably going to find a lot of this code really really advanced and most likely you probably found it really confusing as well especially if you're not used to using the syntax for creating generics instead of the Swift programming language but one thing I do encourage is that you actually try to type out some of the generic syntax that way you get more comfortable with exactly what the code used to look like for it to compile and finally the thing about Erick's is that they're actually super powerful but only if you know how to use them correctly okay so I think that is going to wrap it up for today's video if you enjoyed it and if you want to support me and what I do on this channel make sure to support me by check out the a couple of courses using the links in the description below if you want to download the source code for today's video make sure to head down to the description as well that's gonna be it for me today I will see you in the next lesson oh my guys
Info
Channel: Lets Build That App
Views: 38,859
Rating: undefined out of 5
Keywords: ios, swift, development, tutorial, learn, xcode, programming, code
Id: K0IO4QvFxnM
Channel Id: undefined
Length: 26min 56sec (1616 seconds)
Published: Sat Jun 30 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.