KnowledgeCity

Swift Intermediate: Enumerations in Swift

In this module, you’ll learn about Enumerations, or enums, which define a type for a group of related values.

In this module, you’ll learn about Enumerations, or enums, which define a type for a group of related values. For example, you might create an enum for a direction that would include all the related values of up, down, left, right. When you need to access the values for direction, you’ll be able see all these pre-set values and use them in a type-safe way through your code.

You’ll learn the syntax of enums and how to use switch to find the right case for your logic. Switching is a technique you can use to control the flow of logic in your program. Using switch with enums is a standard practice of evaluating and navigating these related values. 

You’ll also learn how to make your enums more descriptive with associated values and how use an enum’s raw value. You’ll also learn about the protocol CaseIterable, which can make an array of every case in your enum, and you’ll learn how to make and use recursive enumerations.

Learning Objectives

  • Identify enumeration syntax
  • Describe associated and raw values
  • Explain the CaseIterable protocol and recursive enums

Author: Mark Nair

Duration: 35m · 5 lessons
Level: Intermediate
Language: English

Skills you’ll gain

EnumsFunction ModuleFunctional ProgrammingObject CodeProgramming ConceptsSwift (Programming Language)

What You'll Learn

  • Identify enumeration syntax in Swift
  • Use switch statements to match the correct enum case and control program flow
  • Describe associated values and raw values for enums
  • Apply the CaseIterable protocol to produce an array of every case in an enum
  • Create and use recursive enumerations

Key Takeaways

  • Enumerations (enums) define a type for a group of related values, such as a direction with the values up, down, left, and right.
  • Enums let you access pre-set values and use them in a type-safe way throughout your code.
  • Using switch with enums is a standard practice for evaluating and navigating related values and controlling the flow of logic in a program.
  • Associated values make enums more descriptive, and enums can also use a raw value.
  • The CaseIterable protocol can make an array of every case in an enum, and recursive enumerations can be created and used.

Frequently Asked Questions

What does this Swift module cover?

This module covers enumerations (enums) in Swift: enum syntax, using switch to find the right case for your logic, associated values, raw values, the CaseIterable protocol, and recursive enumerations.

What are enumerations used for in Swift?

Enumerations define a type for a group of related values, for example a direction that includes the related values up, down, left, and right, letting you access these pre-set values and use them in a type-safe way through your code.

What lessons are included in this course?

The course includes the lessons Enumeration Syntax, Associated Values, Raw Values, Using CaseIterable, and Recursive Enumerations.

What skills does this course help develop?

It helps develop skills in enums, function modules, functional programming, object code, programming concepts, and the Swift programming language.

How are switch statements used with enums in this course?

You learn to use switch to find the right case for your logic; switching controls the flow of logic in your program, and using switch with enums is a standard practice for evaluating and navigating related values.

Transcript

Show transcript (free preview lesson)

Transcript of the free preview lesson. Remaining lessons unlock with the full course.

Hello, my name is Mark Nair and in these lessons you'll learn about enumerations, or enums, or enums in swift. Including enumeration syntax, associated values, raw values using CaseIterable, and recursive enums. According to the Swift Programming guide, an enumeration defines a common type for a group of related values and enables you to work with those values in a type safe way within your code. So let's say you're making a game, and your game character can move up, down, left and right but you don't wanna go diagonal. You could use an enum to group these values together and prevent the errant diagonal move. So when you create an enum, you use the enum keyword and then you add the enum's definition in curly braces, like this. So the keyword, now I'm going to name the enum. I'm gonna call this one direction, for our example of them being up, down, left and right. Now I've capitalized the name because this is a new type. So just like everything else, if it's a new type just capitalize it, in as far as style goes. All right, now curly braces, then all of the other code will fit in here. Now these are the cases that we will use in the enum. These are, these are how we relate the values. So I want to go up, down, left and right. So each of those will have a case, and then up, down. So four cases. Now if I create a new variable, this case I'll just call it character movement. Right now a character movement, Swift doesn't know what the type is. That's why we have the error type missing, and it can't interpret that. It can't really infer what it is from any context. So let's give it some. Now I'll make it equal to our enum direction, and then I'll use dot syntax dot. And then here I can pick from my different cases. So let's just go to the right. Now, if I look at my character movement by option clicking on it, I can see var character movement is of type direction. Direction of course, right here, is what we defined is our enum. Since Swift knows that character movement now is of type direction, I can easily reallocate, do something like this. And instead of saying direction dot right, I can use a shortcut of just saying dot. Now this works just because Swift knows the type. Swift doesn't know the type, it doesn't work. Here's another enum example. All right, so I have club's, heart, spades, and diamonds. The normal playing card. Magnify this a little bit. You can also determine your cases this way on one line, so we don't have to use case all the time. I could just separate it by comma. It's just stylistically what you like. And the same thing goes for if my card, now I could define it I could say my card and then using dot syntax. Just doing that. So I can be explicit in the definition. And doing that, I can use my shortcut right here. Or if I'm not explicit, of course I would have to say. Either way, then Swift knows that my card is of type suits. And that's how we do that. Now we can use the switch statement to go through these different values. Enum of city with four different cities, vacation spots. I just want four selections. I don't want anything more. I don't want infinite. So just Paris, Tunis, Wellington and Prague. Now I'm gonna make a function where I can travel and determine what city and then have the code respond to what I do in the function. So. So, here my function name is travel. I'm using the function label to, then destination. And then city is what I'll be using from the enum. Now I will be switching on this parameter for this argument. So I'll use the keyword switch, then destination, and then here I will set up my different cases. Now the nice thing is here I have an error. Switch must be exhaustive, which means I have to go through every single case, or have a default case that takes care of all the single cases. But I know I only have four cases, Paris, Tunis, Wellington and Prague that I've defined here on line 21. So I'm gonna use the Swift, very helpful error here. Do you want to add missing cases? Yes, I do. And Swift very helpfully adds the code for case, case, case for the different ones. And then now all I have to do is put in the code that I wanna run when that case happens. Let's call the function. So I will call travel. Now in my handy little popup here I have travel to, now I can put the name of the city. Now, if I just type a name of a city this will not work because it's not referencing the Enum. If I type, say for example New York and I run this code, can't convert the type of string to expected argument type of city. Because remember, city is the enum type they're asking for in the function parameter. Instead, it's very easy travel to, and then I just period on the keyboard and I get my options right here. So I'll go to Prague, and now when I run it, welcome to Prague. Dot Tunis. Welcome to Tunis. So that is how we create, build out, and then use some of the syntax around Enums. Thanks for watching. In the next lesson I'll show you how to use associated values in E-enum.

Learn on the Go

Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.