Understanding associatedtype in Swift

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to awesome sharp weekly in this video i'm going to discuss what exactly is associated type and how you can use it in your swift applications associated type is more of a feature of adding generics to your protocol so let's go ahead see that what do we mean by that adding generic support in your protocol let's say that we are building some sort of a http client or some sort of a service class and we have different services we have movies service or movie movies or movie service we have user service and all of these services should have few functions together so they will share the common protocol which means get all get by id and so on get by id all right so we will probably create a protocol which can be anything i'm just going to call it web service protocol but you can call it anything you want and movie service and user service is going to be conforming to the web service protocol this means that they both need to implement get all and get by id so let's go ahead and do this kind of a scenario and see that how we can use the associated type so i'm going to go ahead and create a protocol and i will call it web service protocol okay and this protocol can only be available on the classes so i'm just going to say over here that class this means that the only person who can have this protocol meaning you cannot really create a structure and then conform the structure to this protocol because this protocol can only be applied to classes and that's it which is fine and now what we will do is we will create our function and we'll say get all we will pass in the url that's fine i mean a url can be different when you're when you're getting all the movies when you're getting all the users you will have different urls so that's why we're passing the url and we will have a completion because probably it's going to be an asynchronous event we will use a result type of i guess in soy 4.2 or something and we have to pass something over here and the second argument can be error maybe something bad actually happened and this is not really going to return you anything okay now if i go ahead and create a movie service class i can go ahead and conform the movie service to web service protocol this means that i need to implement get all but there's already a problem with the get all function definition in the web service protocol and what exactly is the problem well the problem that you're facing is that we don't know what type is it going to return now we can always go ahead and change this we can say get all movies and we can go ahead and return movie object or a list of movies kind of like this i mean there is no movie class we can go ahead and create a movie class if you want there we go simple movie class but that's not what we wanted to do we wanted to simply create a function called get all and provide the type over here that that type can be movie but that type can also be user so if i go ahead and create another structure called user and we will simply say the username that's it so how can i accommodate that i mean i don't want to create another function which is get all user so this is not not good so what can i do i have to provide some sort of a type over here that is coming from or that is determined from who is using this protocol but how do i do this so this is where the associated type come into play it acts as a generic for your protocol so i can go over here and i can say associated type and we will simply call it model you can call it anything you want be a little bit more descriptive so i'm just using model and this time we can just put it right here so i can go over here and i can say model now where is this type coming from well this is coming from the actual class that is conforming to the web service protocol let's go ahead and see so if i go over here and i implement my movie service now obviously the movie service is not conforming to the protocol web service protocol because we are missing the model and we are missing get all movies so let's go ahead and implement some part of it there we go you can see that we are now asked that hey you need to provide a value for the model and okay the value for the model is moving or the type for the model is actually movie this means that when we use when we call the get all movies function which is in the web service protocol the model which is right here will be replaced by type movie now i can go over here and implement get all movies and you can see that now it is movies already injected now one other way is to not provide the type also but simply provide the type over here and this is kind of like the same thing it's basically saying that well the type for get all movies for uh from coming from the web service protocol will be moving so we don't explicitly have to provide the time but i would rather provide the time so that everyone will know that this will be returning that the model will be the movies all right so this is one way to to do those kind of things now if i go ahead and create another class and i would say user service and it is also conforming to the web service protocol i can go ahead and provide type alias or i can simply go ahead and provide well it shouldn't be called get all movies right i mean let's call it get all there we go so i can provide the type alias over here if i want to or i can simply implement the get all function and provide the type over there that's perfectly fine i'm going to say model and the value of model will be a user this means that when we are calling the get all function we are explicitly setting the type of model to be user now again this is not really required you can eliminate this and simply call like in the get all you can see that we can just replace it with the user if you want to that's also going to work so it's completely up to you so there we go using the same protocol which is the web service protocol we have implemented the concrete classes user service and movie service and they both are using the same exact definition which is the get all function and injecting the type which is a generic type later on by providing a type alias so this is how you use associated type in swift language if you want to support my channel then the best way would be to check out my udemy courses i have many different courses on many different technologies usually are mostly ios you can see that i have a course on surf ui declarative interfaces for any apple device this is the best-selling course and the most complete course 21 plus hours of surf ui then i have a second course which is right over here which is just got released and this is about mvvm design pattern in surf ui so how to create applications instead of ui using mvvm design pattern i have courses on rx swift of course is on flutter if you ever want to get started with flutter and also i have a course on server side swift using vapor so if you want to run your swift on the server that's the course for you and the course goes on and on you can see very very important calls json partsync very very important because you do it in every single ios app jsonparsen to get that course also the best way to get this course is to check out the youtube description and you will find the links to all of these courses click on the link and get the course if you also like there is a link of patreon on the in the youtube description if you want to become a monthly subscriber monthly donation that will be great too anything else thank you so much and i really hope you enjoyed the video
Info
Channel: azamsharp
Views: 2,805
Rating: undefined out of 5
Keywords: swift language, ios development, swift tutorial, swift programming, swift
Id: ewQrxyClw5g
Channel Id: undefined
Length: 9min 38sec (578 seconds)
Published: Thu Sep 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.