In Swift, enumerations can be expanded into very complex, and helpful, types. In this video, learn how to add raw and associated values to our enums.
- [Tutor] Enumerations can be expanded to include raw and associated values. Which makes it possible to create more complex and useful enum types in your code. Lets declare a new enum here called NPC and to associate a raw value type, we just add a colon and the type we want. So in this case we we want to use a string. And then open and close curly braces. Lets define some cases here for our NPCs.
We going to say there is a villager, and for the raw value we going to put in a string. Common to villages , not much useful info. We are also going to have a chief, and we are going to say one per village, will have quest info. And in our last case there is going be a blacksmith, with a raw value no limit per village, will make you cool stuff.
Now lets use this, am going to create a new variable called blacksmith, am going to use our NPC enum dot blacksmith. And am going to print out our blacksmith's raw value. No limit per village, will make you cool stuff. Now with raw values and associated values, we still have access to an enum hash value. Associated values are a little more complicated. Lets create a new enum, called PlayerState.
Open and close curly braces. Now we going to have a case for Alive, a case for KO and a case for if what's Unknown. So this looks like a basic enum, with no raw values and really no nothing. But here is what we going to associate values. So here for our KO case, am going to open a parenthesis and am going to add in new variable, restartToLevel of type int.
Am also going to just fill this out for our Unknown and then we will talk about what this is actually doing. Debug of type string. These are called associated values because with each enum case, we are going to be capturing a new value. So in the case of KO, we are going to be capturing a restartToLevel integer and for our case Unknown, we going to be capturing a debug string. Now this might not be super clear without actually using it. So lets declare a new variable called currentState.
We are going to assign this PlayerState.KO. Now you will see an X code that KO is expecting an integer value So am going to say if you are KO-ed you have to restart to level 1. Now here is where the usability really comes in. Lets say we had a switch statement, and we are going to switch on our currentState. Just to be a little quicker we going to wait for X code to pop up our fix it.
And yes I do want to add all my missing cases. That's so nice. So for our case Alive, am going to print out , I'm still here, and for our case KO, we going to print out nothing until we fill this out and get rid of this errors and am going to do the same thing for debug. Since our PlayerState has an associated value, this switch statement can actually capture it in its own block of code.
So in our KO, am going to write, whoops, you will have to start again at level , and here we can use this associated value. The same thing can be done for the Unknown case. Here am just going to print, sorry we are experiencing some difficulties and then just use the debug associated value.
Perfect now on our debug log, we get whoops you have to start again at level one. So lets say we want our unknown state to execute, am going to say, Error incomplete transaction. We will let this run and there we go. Sorry we are experiencing some difficulties and then our debug string. I hope you can already see how useful this kind of enum is.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs