Home » How - To / Tutorial » Programming » Enumerations in Swift Programming

Enumerations in Swift Programming

If you want to define a common type for a group of items or values which are related, then enumeration will help you to do so. It supports some of the features which were traditionally supported by classes only. These features include instance methods and computed properties. Initial values of members can also be defined by the use of enumerations.

Swift-Programming Enumerations in Swift ProgrammingThey have a wide range of uses in Swift programming, and this explains why you need to understand how to use them. Most programmers view enumerations as complex, but in real sense, they are not.

Create a new Swift playground. In Swift, they are usually referred to as enum, so don’t get confused when you find this. Let us create an enum that will show the possible ways to exit from a house:

The values inside {} (curly braces) are called the member values. To define the member values in an enum, we use the case keyword. This shows that a new line for defining a member of the enum has been started. The enum above will contain only the listed above as members. Other than defining them individually, they can be defined in a single line, and you separate them with a comma as follows:

Notice that the name of every member starts with a capital letter. To use the enumeration above, we can do as follows:

Notice that we declared a variable and assigned a value to the same variable at the same time. Again, if you were keen on autoComplete, you realized that it gave the options of only four doors. This is one of the ways used in enums to ensure accuracy.

Apart from the method mentioned above, we can also use this one:

It is very clear that from the second method, we have used specification so as to set the value. Let us demonstrate the use of switch statement to access the values of an enum:

The output on the playground will show that Door1 was used. Notice we have specified a case for every member of the enum, as failure to do so will result into an error. You can delete one of the case statements above and see what will happen. The compiler will tell that your code has a problem.

Enum Associated Values in Swift

With associated values, extra information can be added to each value of the enum. These values can be of any type. To demonstrate this, consider the enum shown below:

We have defined two case statements, one to hold a string and the other one to hold three values of type integer. Let us define two variables as shown below:

Let us to check the values using a switch statement:

Now we have accessed the values. We started by accessing the name, and then lastly we accessed the integers. Every value of the enum has been provided with a case.

Raw Values

With raw values, you are able to declare values inside the enum. It works just in the opposite way as associated values, since these allow you to declare values outside an enum. The type of enumeration to use the technique must be specified.

Note the syntax we have used to access the values. Very easy. You should know how to handle enums without values, as you might try to access empty values.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *