How to use Typealias in Swift | Continued Learning #19

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right welcome back everyone i'm nick this is swiffle thinking and in this video we're gonna look at type alias and it's a very fancy word it looks like it's going to be complicated but it's actually super simple so this should be a pretty quick video basically we can use type values in our app to create a name for an existing type so this is a great way to create sub names of existing models and existing types in your app that might be a little more applicable to the part of code in your app that you're working on again it sounds hard but it is super easy and you don't really use this too often in your apps but i'm going to use it in the next video so i just wanted to briefly introduce it to you guys all right everyone i am back in xcode and this should be a pretty quick video i just want to touch on type alias so let's right click the navigator create a new file it'll be a swift ui view let's call it type alias bootcamp i know it's a weird word you've probably never seen it before i know i didn't until i actually learned what it was but it's actually pretty simple to use let's hit resume on the canvas to make sure we're all connected and very simply here i'm only going over this because we're going to use it in the next video let's pretend like maybe we're making an app like netflix and maybe all across our code we had a model for a movie so we're going to create a model and if you're not familiar with models i did a whole video on that in the swift ui boot camp so i'll link that below that you guys can go check out but very simply we're going to pretend like we have a movie model in our app so create a struct we'll call it movie model open the brackets and every movie is going to have a title so we'll say let title of type string every movie will also have maybe a director so we'll say let director of type string and then maybe every movie has like a count of how many people like the movie just let's just pretend so we'll say let count of type int okay now let's just put one simple movie into our app so down in the view here let's do at state var and let's call this maybe item and it will be of type movie model and we can add a movie model here so we'll set it equal to a movie model open the parentheses and it's going to ask us for our initializer for the title director and count so we'll say title the director will just put joe and then count will be five and in the view very simply i'm going to make a v stack and we're going to put a text we'll add the item dot title a text with the item dot director and a text with and we'll make it a string we'll use the backslash open close parenthesis and we'll put in the item dot count by click resume on the canvas we should see very simply our uh our item onto the screen so we got the title that we added just the word title we have a director and we have a count of five so obviously here we only use this movie model once but let's pretend like we had the netflix app and we use this movie model all across our app hundreds and hundreds of times and then all of a sudden we want to add a new feature to our app and we want to add let's pretend like we wanted to add tv shows and these tv shows are also going to have the exact same information as the movie model so the tv shows are going to have titles directors and counts well the easiest thing you would think you could do is create a new struct so we could create so i could copy the struct paste another one call it a tv model and this will be for our tv shows and then instead of using this item i could create an at state var item of type tv model and we'll set it equal to a tv model and we use the word and we use the title we'll say tv title director will be emily and count will be 10. if i click resume it obviously will still work we have our tv model we have two totally separate models here and this is fine but if there were places in our app where we were using functions that were based on a movie model well we would then have to rewrite all of those functions to be based on a tv model as well we couldn't just reuse those functions because these types are different and additionally if we wanted to make an edit to the movie model we would then also have to go in and make an edit to the tv model so although this works there's a more efficient way to do this so we can do is basically instead of using the struct and i'll delete it we can do a type alias and here we can put the name that we want so we'll call this tv model and we can set it equal to another type and here i'm just going to set it equal to a movie model so now we can initialize this item as a tv model and we can initialize it the same way where we call tv model open the parentheses and get that completion except all this completion this data is actually referencing the movie model's completion because we set a tv model actually equal to the type of movie model so that's what type alias pretty much does it's basically creating a new name for an existing type and this comes more in handy when you have larger applications so if there's like a section of your code where you want to refer to a specific type as something other than the actual name so maybe you had like a user struct and then in one part of your code you wanted to refer to the user as customer instead of a user well you could create a type elios of customer and set it equal to type user so that's it for this video that was a pretty simple one i just wanted to briefly introduce you guys to this type alias you're really not going to use it too often in your code i've done many projects where i've never had to use it but it does come in handy and in the next video i'm going to quickly show you one of the more common ways when we use this so i just wanted to quickly introduce you guys to it first all right thank you guys for watching as always i'm nick this is swiffle thinking and i'll see you in the next video bye
Info
Channel: Swiftful Thinking
Views: 884
Rating: undefined out of 5
Keywords: SwiftUI typealias, typealias, Swift typealias, swiftUI what is typealias, swift what is typealias, SwiftUI how to use typealias, Swift how to use typealias, SwiftUI how to implement typealias, Swift how to implement typealias, What is typealias in Swift, what is typealias SwiftUI, type alias meaning, what is typealias, typealias definition, help with typealias, typealias not working
Id: wBd3s4ZW8Ps
Channel Id: undefined
Length: 7min 3sec (423 seconds)
Published: Mon Apr 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.