Swift Enum - Basics, Raw Values, Associated Values, CaseIterable & More

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're talking about the fundamentals of enumerations or enums for short we're going to talk about how to declare them what they are how to use them with a switch statement how to use them with raw values and associated values and then also how to use them as constants to help your code be more readable but first today's video is sponsored by dev mountain devmountain is an in-person coding and design bootcamp that offers housing at no extra cost for full-time immersive students now if you know my story i myself am a boot camp grad it was part of the process to help launch my ios developer career which i'm going on year five now in this wonderful profession but aside from their ios development program devmountain also offers programs in web development software qa and ux design they even have a career services team to help you with job placement and financing options are available and devmountain loves hearing from my viewers so if you or someone you know is ready to start this journey into ios development be sure to check out the link in the description alright back to the video here we are in an empty playground let's start off with what an enum is uh real simple it's a group of related values let's declare one and that'll make more sense enum and then you name it and this should be capitalized that is convention so we're going to call this social platform and you'll see we're going to do the group of related values so an enum has to have cases so the first case is going to be twitter second case facebook second case instagram and you can see how these are a group of related values right another common thing is like directions north east southwest you know that kind of stuff so case i'll do linked in and one of the most basic ways to use an enum is with a switch statement to do something based on whatever case you pass in let's write a function to demonstrate that so here we're gonna do uh funk share image and we're gonna do on platform and then we wanna pass in a social platform right so now we're gonna pass in either.twitter.facebook.instagram and based on that enum value we're gonna do the proper code to actually share that of course we're not gonna do all that code here this is all about the enums though so let's let's talk about that so we do switch on the platform right that's what we we pass in here so platform and then i'm going to wait a second because i'm lazy and xcode's going to do this for me right you see here a switch must be exhaustive so what this is going to do for me is it's going to fill in all my cases with the enum because it's based on this social platform now again each case has to have some code here so like i said this is where we would do the code to actually share the image on the specific platform but i wanted to demonstrate was how you pass in a specific uh enum value and then based on that value you do different stuff let's actually get rid of these uh errors real quick this is where the code would go to share the image on twitter i'm just going to copy paste this again we're just getting rid of these errors um like i said we're not going to actually write out all the code to do the sharing of the image the social platform but you get the idea here we'll scroll up here on facebook on instagram linkedin just to show you that okay so now when we actually call this function watch how well it reads because we use this enum and then it will demonstrate that it goes into the specific case so here we can do we can call share image and you see on and then it takes in a social platform so we would pass in and here's the cool thing you do dot and you get the auto complete another benefit of using an enum in this pattern here is you know you can't just pass in anything you've got to pass in one of these four values so we're gonna go down to twitter cool so look how well that uh the call site of the function reads share image on twitter doesn't even sound like code so let's run this and you'll see down here it should print you know this is where the code would go to share the image on twitter and then if i change twitter to dot linkedin again same thing this is where the code would go to share the image on linkedin of course the print statements are just placeholders but that is the fundamental basic way enums are used most of the time but now let's talk about other ways you can use them like i said constants raw values associated values let's start with constants now this example is from my github followers course if you're interested in that course sean allen.teachable.com but in this course we're using sf symbols and you can see i created an enum called sf symbols where we can access the symbols we're going to use very simply so here you can see i do static let location equals uiimage system name map pin dot and ellipse that's the name of the sf symbol whereas if i didn't have it in a constant like this i would have to declare you know map in dot at ellipse i'd have to write out that string every time so here's where we're declaring the enum with static constants on the enum now watch how we use this and by the way just for clarity this is the screen we're talking about you can see how we have sf symbols for like repos and gis and following and followers the little heart the little people those icons are sf symbols so you can see how we're using it here in the code right based on if it's a repo just followers we can just call sf symbols.repos why can we do that it's because we've set that constant and like i said if we didn't have the constant we would have to type out that ui image you know the string that declares what sf symbol it is but by putting into an enum into a constants file anytime we want that sf symbol all we got to do is do sfsymbols.gis sfsymbol.followers so this makes your code very readable and less error-prone because you don't got to type out that string every time because as you may know if you have a typo in that string you're not going to get that image so that's enums as constants let's move on to enums with raw values so back to our enum definition up here at the top we'll get rid of that bottom console so here in the enum we can declare a raw value type and essentially what a raw value is it's attaching a value to the enum case let's do this and that'll make sense here so let's say our social platform has a raw value of a string and we're going to get my opinion on each of these platforms so i can attach a string to this case saying you know this is my opinion right twitter this is my favorite cool uh facebook is going to equal i never use this instagram pretty pictures and finally uh linkedin i need to start posting here right so those are my those are my opinions on the uh social platforms you know what i'm about to do if you follow me gotta line them up so now we've attached this raw value of a string to the cases right so let's talk about using these and how we get access to the raw value is we would do twitter.raw value as always let's talk about an example so we're going to create a new function here under here we're going to do funk get shawn's opinion and then on it's the argument label platform and we're going to pass in again a social platform that we've declared up there right we're passing in the enum so let's get that raw value as we pass that in so i can do let opinion equal platform this is the parameter we're passing in which again is either going to be twitter.facebook.linkedin platform.raw value and you can see it's telling me that is a string it doesn't have to be a string you can declare it as an int double whatever you want to declare it as but in this case it's a string and we're going to do print opinion okay and i just separated those two out so you can see what was going on of course you could just do print and i'm going to do this now to clean things up but you can do print platform.raw value but i wanted to break that out so you could see what i was doing so now when we call get uh sean's opinion on social platform let's say dot facebook and again look how nice that reads get sean's opinion on facebook and if we run it now we see well let's comment this uh function call out too so we're not getting that extra print statement run it again so it's nice and clean down here i never use this so shawn's opinion on facebook is i never use this now let's pass in uh dot twitter so again what's happening when we call this function we're passing in uh.twitter as the platform and then whatever platform we pass in we're getting its raw value and again the raw value is whatever we attach to it up here and we said it's a string so now when we get sean's opinion on twitter we're gonna run it this is my favorite so that is how you use raw values again you declare the type after the enum and then you attach you know that type to each case and then you can access whatever that raw value is by doing twitter.raw value in linkedin.raw value next up let's talk about case iterable where we iterate over all the cases in an enum in order to get access to case iterable though you have to conform to it so after string let's conform to case iterable protocol there you go so now we have access to what is called all cases which does what it says it puts all these cases into a collection and we can use it just like a collection so let's comment out get sean's opinion so we don't get those extra print statements and let's actually uh let's do print uh social platform dot all cases remember we get access to this because we conform to case iterable and now this is just like any other collection we can do dot count so how many cases are in social platform so we can just do all cases dot count should print off four there you go you see it does print off four and like i said we can use this like a collection so we can also use this like a for loop so i can do four platform in social platform.all cases and we can do print let's do platform.raw values this is going to print out all the raw values we had attached to that platform.raw value so now when i run this we're going to iterate through all cases of the enum and print out the raw values there you go this is my favorite i never use this pretty pictures i need to start posting here and again just to reiterate these are the raw values up here for that so we talked about raw values we talked about case iterable we talked about the basic way to use enums the last thing i want to talk about is associated values with enums and for that let's make some room here let's actually get rid of this bottom one let's comment this out so we don't get a cluttered print statements here and actually to avoid confusion i'm going to create a new enum called social media platform just because i don't want to like go back and change all this and it would mess up all the raw value stuff so to keep things clean i'm going to create a very similar enum just name it differently so let's do enum call it social media platform again we're just naming it a little bit different so i don't get a duplicate naming here not platform platform and again we got case twitter let's add youtube in here this time case youtube case instagram case [Music] in okay now we're going to add associated values uh so let's say we want to come up with a function where we want to see if we're eligible to get a sponsorship based on like follower numbers or subscriber numbers right let's pretend a company says you must have x amount of subscribers before you're eligible for sponsorship so what we can do is we can add an associated value to the cases and you can name it here so we'll call this followers and that is an int you got to give it a type and we'll do youtube subscribers that is also an int now i could do this all the way down with instagram followers and linkedin connections but i'm going to leave it as just youtube and twitter because i want to showcase how you don't have to have associated values for every single case in your enum in fact only one could have it if you want so a little bit of a convoluted example but i want to leave those bottom two blank just to illustrate that point so let's write our function to check for sponsorship eligibility funk get sponsorship eligibility and then you can do for and then platform again same thing and now this is not platform platform and then now again social media platform let's be careful not to make it our social platform like we had before and here's our function sponsor ship misspelled thanks to the xcode underlying for the misspelling so now what we can do in switching off this platform we're going to have values associated to twitter and youtube switch platform case dot twitter and we can do let followers so i have access to this followers variable that we attached to it and what i could do down here is we can get a little messy and do if you know followers because i have access to this now uh is greater than 10 000 right we can have some nested if statement or we could use where to clean this up a little bit so you can do some pattern matching here so if the case followers where followers is greater than we'll do 10 000 and that underscore is just to space out the zeros it means nothing you don't have to do anything it just makes it easier to read what number it is rather than having a bunch of uh zeros after it and x goes yellow notice right now we'll fix that so in this case uh we want to print eligible for sponsored tweet cool so we're eligible for sponsor tweet now let's do the case of dot youtube and same thing let subscribers and then where subscribers is greater than let's say this is 25 000. of course these are just made up numbers i'm not saying you have to have this much of a following to get a sponsorship not at all and let's copy and paste this print statement to save a little bit of time here paste that eligible for sponsored we'll say video so what happens is when we pass in twitter if the followers that we pass in and you'll see we're going to call the function in a second if it's greater than 10 000 we're going to be eligible if not we're not going to be eligible so let's get those cases where we're not eligible for sponsorship so that is going to be case instagram right and we can put all these on one line too uh case linkedin right because those are the two that we don't really have a good following on so we're not even we don't want to be eligible for that at all but now if we just do a plain old twitter with no you can delete this stuff with no associated value here that's kind of like every other twitter uh case that doesn't already fall into one of the top ones and that's a good point to bring this up the switch statement evaluates from top to bottom so if we had this last case here at the top it's going to catch them all and we're never even going to get to check the count of the followers so let me finish this out and you'll kind of see what i'm talking about once we start running the function uh what's the last one dot linkedin no dot youtube and again we can get rid of the uh parameter there and if this is the case let's paste that and we'll say not eligible for sponsor ship so let's actually call this function and we'll start running through some scenarios so you can understand it i'm going to space this out so you can read it a bit easier let it breathe a little bit okay so now when we call the function here say get sponsorship eligibility for let's pass in twitter.twitter and you can see we get this parameter here now we can pass in whatever number of followers we have so let's say uh 15 000 followers so now when i run this we should be eligible for a sponsorship right because remember the switch statement evaluates top to bottom so when i pass in twitter with 15 000 followers it's gonna go into this first case because it's the case twitter and the followers are greater than ten thousand well it should let's run it to test that it should say eligible for sponsor tweet yep eligible for a sponsor tweet let's change this let's take a zero off of that and say we only have fifteen hundred followers so again what's going to happen it's going to go into this switch statement it's not going to fall into this case because we're not greater than 10 000 followers right we satisfied the twitter part of it but not the followers over 10 000. so it's not going to go into youtube so where it's going to go into is this one this kind of catch-all where it's like plain old twitter so now when we run this it's going to say not eligible for sponsorship there you go one last test for youtube just to show this is working dot youtube and let's give us a hundred thousand subscribers we'll get there someday hit run and we should be eligible for a sponsored video and again just for tests let's make it 10 000 here and because we shouldn't satisfy that and we should not be eligible there you go not eligible for sponsorship so that's an example of how you can use an associated value on your enum if you like my teaching style check out the website on the screen i started creating my own courses we'll see in the next video you
Info
Channel: Sean Allen
Views: 26,830
Rating: undefined out of 5
Keywords: Swift, Swift Tutorial, iOS Developer, iOS Development, Swift Code Tutorial, Swift app tutorial, Swift enum, enumeration in swift, swift enumeration, swift enumerations, swift enum associated values, swift enum tutorial, enum vs struct swift, enums in swift, enumerations in swift, enum in swift, enum swift, enum swift 5, enum swift tutorial, how to use enums in swift, how to use swift enums, swift keywords, keywords in swift
Id: CdBL7m1AeII
Channel Id: undefined
Length: 15min 55sec (955 seconds)
Published: Thu Apr 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.