How to use Protocols in Swift | Advanced Learning #15

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay we are officially in the final stretch of this advanced playlist so if you're following along congratulate yourself that you've made it this far but do not get lazy because this is probably the most important section of this playlist we're going to cover some topics that are just so important and so crucial to actually make really good ios applications and the first topic of course is going to be protocols that we're covering in this video and if you are just joining us i do want to welcome you to the channel my name is nick this channel is swiffle thinking where we cover all things swift and swift ui related and i want to remind you to hit that subscribe button if you want to stay updated on all these videos and all things swift now as i mentioned this video is going to cover protocols we're going to first review what protocols are how we implement them and then we're going to go over some more advanced examples now the word protocol probably sounds a little more challenging a little more daunting than it actually needs to be a protocol is just a simple set of rules or requirements that a struct or class needs to have now in swift to create a protocol is actually pretty simple all you have to do is give it a name and then inside the protocol we list out all of the requirements now these requirements are generally just variables and functions that a class or struct would then need to have and if you have been following this playlist or any of my playlists or even just writing code in swift ui chances are you've actually been using protocols all along so for example in the swift ui every time we make a new view it is a struct we give it a name and then we make that struct conform to view and as i've said a million times every view then needs a body and the reason for that is because of view is actually a protocol and the requirement of the view protocol is that the struct has a body so we've been using protocols all along of course when we create our own protocols we can give it custom names we can give it custom requirements so in this video we're going to look at a couple examples and then in the next couple videos we're going to actually use protocols to really efficiently add dependency injection and then testing into our apps all right welcome back everyone so i'm back in our xcode project uh and let's right click the navigator create a new file for this video it's going to be a swift ui view as always and we're covering protocols so let's call it protocols boot camp go ahead and click create once you're inside click resume on the canvas and let's get coding we're not going to need the navigation bar so i'm just going to close it on the left side here just to make the text a little bit bigger and let's get coding and we're going to start this off with a very very basic example and if you've been following my courses you're kind of already aware of this but a lot of times when you make an app you actually want to set up a definitive color theme this way you use the same colors across your entire app so let's pretend like we have a color theme in our app i'm going to create a struct and i'm going to call it default color theme i'm going to open the brackets and our color theme is going to have three colors we'll say let primary and this will be of type color of course and we'll set it equal to blue let's say let's secondary so type color and we'll set it equal to maybe white and then we'll do let tertiary of type color and we'll set it equal to maybe gray so when we create our view let's pass in the color theme for our app so here i'm going to say let color theme of type default color theme and we'll set it equal to an instance of default color theme open and close parenthesis now in your actual app you probably wouldn't initialize a new color scheme in every single view you would probably initialize it once in your app and then inject it into the view but we haven't learned a dependency injection yet so for right now all i want you guys to worry about is that we are getting a color theme in our app and let's prove that by actually creating the view with the color theme so here i'm going to create a very simple view let's create a z stack and let's make the background the tertiary color so here we're going to reference our color theme so we'll do color theme we'll call dot tertiary and let's make it dot ignores safe area so it goes to the edges of the screen click resume so we should have gray background and then we'll just add a very simple text that says maybe protocols are awesome and let's give this some colors so let's make the font of headline let's give it a foreground color of color theme dot secondary let's give it some padding and then maybe a background color and we'll use the color theme here of dot primary which is our blue and then finally dot corner radius let's just dot corner radius let's just throw in 10. all right so very simple screen here but you can see that our color theme is working we have gray we have our blue we have our white and now the question is what if we are building our app and at some point in our app we wanted to change the color theme maybe we wanted the primary color to be a different color well if all we want to do is change the primary color that's easy we could just change this to red we can resume our entire app rebuild the app and it will work but what if we wanted to change out the entire color theme or maybe we wanted different users to have different color themes how would we do that because then we can't just change this variable because this variable is going to get changed always so let's undo that if we really want to add a secondary color theme to our app we would actually create another struct we would create a struct here and i can call it alternative color theme open the brackets and in my alternative color theme i'm going to do the same thing so i'm going to copy these so i'm going to copy these constants and paste them in here and in my alternative color theme let's just do red we'll make the background maybe orange just so that we can really see the difference and then the tertiary color let's make it green why not so now in our app if we wanted to add the alternative color theme we could kind of do the exact same thing as this except with alternative color theme so i'm going to comment this out and let's just say let color theme of type alternative color theme we'll set it equal to alternative color theme if i click or zoom and build and run the app it's going to work we have our new color theme the orange actually doesn't look very good so let's keep the text of white maybe but the key thing to focus on here is that when we switched out this color theme for this color theme we actually had to change the type of this variable right so the type here this is of type alternative color theme this is of type default color theme so the problem here is that because these are different types they're not exactly interchangeable so for example if i kept the default color theme here and i wanted to put this as an alternative color theme it's going to crash it's going to tell me that we can't set this as we can't set an alternative color theme as a default color theme and that's obviously because they're different types now we know that the alternative color theme and the default color theme have exactly the same contents right so they both have primary secondary and tertiary that are type colors so they should be interchangeable but swift does not recognize that the content in here is the same as the content in here just with different values so this is where protocols start to come in so what we're going to do is create a protocol and if i type in protocol here i can add a name for our protocol i'm going to call it color theme protocol i like to add the word protocol at the end of every protocol just so we know that this is a protocol of course so in protocols basically just a set of rules or requirements that any structure class needs to have so for example every time we create a view and i've done this hundreds of times in my videos every time we create a view it makes us add a body and that's because view is actually a protocol and part of that protocol is that a view needs to have a body so this is going to be the same thing here we're going to create a color theme protocol so that anything that conforms to this protocol needs to have whatever these requirements are so the first requirement is going to be of course the primary color so here i'm going to put let primary and it will be of type color and when we do this we're going to get some errors here the first error we can click on this is that protocols cannot require properties to be immutable declare read-only properties by using var with a get specifier all right so we can go down here and click fix but i'm just going to walk you guys through it quickly so basically we basically what this is telling us is that we cannot use this let constant inside a protocol instead we need to use var and now we're going to get one more error that says a protocol must have explicit get or get set so when we create a variable we know usually the whole point of creating a variable is that the value can change right if it didn't change we would create a let but because in a protocol we can't use let we can instead make this variable get only so that means we can fetch the value but we can never actually set it so we can't actually change it and we make it get only by simply opening the brackets and putting in the word get so now every color theme protocol is required to have a a primary variable that is of type color and it will be get only so that means when we're accessing it from our view we can fetch this color but we can never actually set a new value for this color so once we set the color as maybe red it'll never change if we wanted it to change we could change it to get set as the error had just said but for right now we want to create a color theme that's not going to change during the life cycle of our app so we're going to keep it as get only now i'm going to do the same thing for secondary and tertiary because every color theme needs those so i'll do var secondary of type color and it will be get only and var tertiary of type color and again it will be get only let's comment out our color theme down here and now all we need to do is conform to the protocols so up here i'm going to add that conforms to color theme protocol i'm going to do the same thing on my alternative color theme so now so now swift knows that anything that conforms to the color theme protocol is going to have a primary a secondary and a tertiary variable and all of them are going to be of type color so now on my view here if i create a color theme i can say like color theme of type and instead of using a specific color theme i can use the protocol so i can pass in color theme as type color theme protocol and then i can set it equal to either of these so i can set it equal to a default color theme let's click resume i could also set it equal to an alternative color theme and let's click resume and you'll notice here that i did not need to change i did not need to change the type of the color theme whereas here we had to change the type here we keep just one type so now we don't need to change the types and the system knows that anything that conforms to this protocol is going to have these values and obviously changing it here is fine for our previews but in our actual app we would want to be able to change this programmatically so at the start of our app we could decide do we want to use this color theme do we want to use this color theme and then we can pass whatever is the actual color theme into this view so instead of initializing it here we're just going to pass it into the view alright so now our view every time we create our view we can pass in a color theme and our color theme could be anything that conforms to this protocol so coming down here to our previews i can pass in a color theme and now i can decide before we even create this view which color theme do i want to use so i can use the default color theme here i can click resume and i can use an alternative color theme and i can click resume let's come back up here and create one more color theme so i'm going to create a struct and let's call this another color theme and let's make this conform to color theme protocol open the brackets and when i do this we're going to get that error message that says another color theme does not conform to color theme protocol do you want to add the protocol stubs now you've probably seen this before as we're creating our apps and building out all these other types because everything in swift ui conforms to protocols whether it's v whether it's views view builders view models everything conforms to a protocol you've probably seen this and of course we can fix it by clicking this little fix here when i click that we're gonna get everything that we need to conform to this protocol so you can see here that automatically the system gave me a primary secondary and tertiary color it's exactly what is in the protocol so i'm going to clean this up quickly and just remove the spacing in here i don't know why it added spacing then i can just set this equal to maybe let's do blue do secondary of red and tertiary dot purple and now going down into our app of course we can use an alt we can of course use an another color theme click resume and our whole app is going to work the same way except we can now interchange and swap in whatever color theme that we want and the big thing i want to point out here is that the whole purpose of creating a protocol is to make sure that whatever it is that struck that class that it has all of the requirements for that protocol so here our requirements are primary secondary and tertiary and of course this is not that much so it's pretty easy to copy but just as an example if our another color theme conformed to color theme protocol but didn't have our tertiary value here we're going to get an error now of course the error is that we don't conform to the protocol but the reason that we really need to conform to the protocol is because our app might be looking for that tertiary value so right now down in our app it's it knows that anything that gets passed in as a color theme is going to have tertiary it's going to have secondary it's going to have primary so circling back the whole purpose of this protocol is not just to make sure our code is nice but it's to make sure that what our everything our app needs and requires is always going to be there so it's making sure that whatever is in this color theme is going to have these three values so i'm going to press command undo here and just conform back to that protocol now there's a couple things i want to show you about protocols before we wrap up this video so obviously creating color themes is a great use case for a protocol pretty much any time you're swapping things in and out for each other you should use a protocol i'm going to get much more into this in coming videos and coming courses but before we leave this video i just want to touch on some of the other things that we can do with protocols so coming down to our view i'm going to delete these two lines here because we're not needing them and now let's mess around with another example here so i want to create maybe some sort of data source that's going to it's going to work kind of like a view model that it has uh maybe the text for this button and maybe has an action when we click on this text button so let's create a class here and let's just call this maybe default data source it doesn't really matter what we call it right now and we'll open the brackets and inside the default data source there's going to be a variable that's called maybe button text and it will be of type string we'll set it equal to protocols are awesome so in our view here maybe after the color theme let's add a let data source and it will be of type default data source and now the text instead of typing in the text here let's reference the data in the data source so we'll call data source dot button text and finally let's fix the error so every time we create protocols boot camp we now need to add a data source and the data source is just going to be our default data source for now open close parenthesis let's click resume and make sure it works cool you can still see the text and and of course we're not conforming to a protocol yet so if we had a secondary data source let's create another class and call it alternative data source open the brackets and maybe this one has another var button text of type string and we'll set this equal to protocols are ling and if we wanted to use the alternative data source in our app as you guys know we right now are going to need to change the va the type of the data source because right now it's a default data source so really because we want to swap between data sources this data source should actually be a protocol just like our color theme so let's create a protocol and i'm going to do it up here above the classes just because it will compile better so it's called protocol and the name of this will be maybe button text protocol this is obviously a very simple example but the requirement of this button text protocol is that it has a variable called button text that is a type string i'm going to paste that here and we're going to make it get only so now these classes can conform to button text protocol this can conform to button text protocol and the data source will actually just be of type button text protocol so now when i initialize down in our view here if i initialize a data source we could use a default data source or we can use an alternative data source click resume and we can see now it says protocols are lame i don't like this color theme so let's actually go back to the default color theme and i'm going to change this back to the default data source just because i like i think protocols are actually awesome and let's go back up and keep working here so we have this one variable that has button text but we can also actually add functions into protocols so maybe every time we have this button here there's a on tap gesture and maybe there's some function that we want to execute now we want to get this function maybe from our data source so up in the data source we can create a func that says maybe button pressed open and close parentheses and open the brackets and when we click it maybe it just prints out button was pressed alright so now on our tap gesture we want to reference the button pressed function so i'm going to call data source dot and in here you might expect us to actually get the function that we just made but when we look into the data source the only values we're going to get from this data source are the requirements of the button text protocol right so even though what we're passing into this data source this default data source even though it has this function the only values that this view is going to be able to get are the protocol that we're actually passing in because think of because we're actually just passing in the protocol part of this data source we're not actually passing in the entire class so the requirement of our protocol is that we have a function called button pressed so i'm just going to copy this and paste this here so anything that conforms to button text protocol must have button text and a function called button pressed so we're going to get an error down here that the alternative data source doesn't conform to the protocol now and we just need to add in here our button pressed function so once we do that the error should go away and now if i go down to my view i can access the data source and i can see the button press coming through because again what's coming through the data source is actually the anything all the requirements for the protocol so here i can now add dot button pressed and all is good and the last thing i want to show you guys before we wrap up is that protocols can actually inherit from each other so if i come back up here and i look at my button pressed maybe for whatever reason in my app the alternative data source actually doesn't have a button pressed function maybe we don't want to have a button pressed function so i'm going to actually delete it here and the button text protocol is actually just going to be something that has button text and instead there's going to be another protocol that's called maybe button pressed protocol and the requirements of the broad press protocol is of course that it has the function called button pressed so now the bond text protocol again only has our button text so coming down here we're going to get an error because button press is now not part of the button text protocol so in this situation we would actually create maybe another constant here we can say let um [Music] data source 2 let's just call it and this will be of type button pressed protocol and then we can access the data source 2 on our cap gesture because we know that anything that conforms to the button pressed protocol is going to have a button pressed function and down in our preview we can click fix and we can add in here anything that conforms to button pressed protocol so coming up here we can take our default data source we can conform to button text protocol we can also conform to button press protocol so now i can pass in the default data source as the button press protocol notice that i cannot pass in the alternative because it doesn't conform to that protocol but i can pass in the default so coming up here so coming down here i can pass in default data source and click resume and we can build without any issues and going back up here this is obviously great that the default data source conforms to both these protocols but what if in our default data source what if we really wanted to make sure that whatever our data source was that it conformed to both of these protocols so again the alternative right now only conforms to one but what if we wanted to make sure that all of our data sources conform to both protocols well what we can do is make a protocol of protocols we can add a protocol here and i can call this i'll call this maybe button data source protocol and i'm actually going to leave the requirements in here blank we could add our own custom requirements for this protocol as well but really we want to make sure is that anything that conforms to this protocol also conforms to the button text protocol as well as the button pressed protocol so now if i conform to this protocol it's the same thing as conforming to both of these so down in our default data source we're really just going to conform to the button data source protocol and back down in our view the data source is really just going to be a button data source protocol i'm going to get rid of this 2 here so coming down we can get again let's just make this the data source and let's update our completion here and let's get rid of our data source 2 here click resume and our errors can go away so again looking at this the big thing here is that protocols can inherit from other protocols so our default data source is conforming to button data source protocol which includes the text protocol so we have our button text as well as the pressed protocol and we have our pressed so down in our alternative data source if we wanted to conform to the button data source protocol we're going to get our error message here because in order to conform to that of course we need to add in our button pressed function but the key here is that because there are sub protocols we could always keep the alternative protocol as maybe just conforming to the button text protocol and we're fine all right everyone so that was just a very quick intro to protocols how protocols work why we use protocols obviously it's a little difficult in this very simple video to fully understand the power of these protocols but as we move further in this course and start getting into dependency injection and some more complex things with protocols i think you guys are going to start to see how powerful they can be the big takeaway i want you guys to have here is just that a protocol very simply is a set of requirements and we can conform to a protocol to make sure that that class or that struct has everything that we require it to have so for our color theme again we're requiring anything that conforms this protocol to have these three values and the reason is because when we use color theme protocol in our app our app is going to expect to be able to access this variable this variable and this variable so this way if we go to use one of these color themes in our app the the rest of the app the view is going to know that it will be able to access the primary secondary tertiary at all times there's never going to be a case where we're passing in something that conforms to a protocol and it doesn't have one of the requirements so for example if i take out the tertiary we can see here that we're going to get our error message and that's because we absolutely need the requirements for our app to run add it back in and the error goes away i hope this wasn't too confusing for you guys i know it's a bit abstract but again we're gonna get more advanced as we go thank you guys for watching and if you enjoyed this video if you learned something do not forget to hit the subscribe button leave a like and comment on this video and thank you all for watching as always i am nick this is swiffle thinking and i'll see you in the next video [Music]
Info
Channel: Swiftful Thinking
Views: 1,118
Rating: undefined out of 5
Keywords: swift protocol, swiftui protocol, swift protocols, swiftui protocols, protocols in swift, protocol swift, protocols in swiftui, swift what is a protocol, swift how to use protocol, what is a protocol in swift, what is a protocol xcode, what is protocol swiftui, swiftui how to create a protocol
Id: 0gM1wmW1Xvc
Channel Id: undefined
Length: 28min 37sec (1717 seconds)
Published: Wed Oct 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.