Swift enum With Examples

In this tutorial, we’ll be discussing the basics of Swift enum. You must be familiar with Enumerations if you have a previous programming background. Enumerations in Swift are really powerful and more awesome. Let’s dive right into them!

Swift Enum

As per the Apple Documentation: “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”.

In other words, Enumerations are lists of things. Enumerations in Swift are first-class types in their own right. They adopt many features traditionally supported only by classes such as defining functions within them.

Swift enum also allows us to define our own data types as well as handle various types of values. Let’s look at Swift enum syntax.

Swift Enum Syntax

An enumeration is defined using the enum keyword followed by the name as shown below.

We can add different values inside the enum as shown below.

The values inside an enumeration are defined using the keyword case. All such values are known as enumeration cases. Instead of defining every enumeration case separately, we can do it in a shorter way as shown below.

Note: As per the guidelines, Swift Enum name and values should begin with a capital letter.

An enum value is specified to a variable in the following way:

Once the value is defined you can reassign it without the need to specify enum name again.

Swift lets you auto-complete the value name from the list of cases defined.

Using switch statement with Swift enum

Note: default case in switch is not required since we’ve covered all the enum cases in the above code.

If some of the enum cases aren’t covered we’ll need a default for sure then as shown below:

Function inside an Enum in Swift

We can define a function inside an enum in swift programming. Following is a function defined that sets the default Enum value as one of the cases:

Enums are value type and not reference type

Enums values are passed by values. The following code demonstrates an example:

Associated Values

Associated Values allows each case to have one or more types (e.g. Int, String, Double) that the enumeration cases can use.

That’s pretty awesome! Using Enums we were able to reassign a variable to different types and extract the associated values each time.

Initialising Enums With Types

The Syntax to initialise an Enum with types is given below:

  1. In the above code we’ve defined an enum with type String.
  2. This allows us to assign a value to cases in the enum block itself rather than doing in switch statements like we did previously.
  3. These values assigned to cases are known as Raw Values which have the same type as that of Enum(String in above case).
  4. These raw values can be returned when called upon the enum cases as DaysOfAWeek.Sunday.rawValue.
  5. A case which doesn’t have any Raw Value defined would consider the case name as the raw value.

Swift Enum Raw Values doesn’t exist when the enum type is not defined.

Let’s simplify the switch statement now that we have values stored as raw values:

Retrieve Enum case from Enum Raw Value

The Enumeration case can be retrieved from the rawValue in the following manner.

We pass the rawValue inside the DaysOfAWeek standard init and an optional is returned. Refer here on Optionals.

Auto-set Raw Values

Raw values can be auto-set for enum cases if it’s set for one case as shown below:

Convert Enum case to String

Enum HashValue vs RawValue

All enum cases have a hashValue which is like an index of the enum cases in the order in which they are defined. The index starts from 0. HashValue exists for enums with types and enums without types. On the other hand, rawValues are used to assign a value to enum cases. RawValues exists for enums with type only.

Optionals Are Enums

Yes indeed. Let’s see why?
Swift Optionals are a type with two cases. Either the value exists or it doesn’t. If the value exists the Optional wraps the value as Optional(value).
Let’s see how an Int Optional looks like:

Essentially, an Optional is an Enum with two cases that we can define:

  • NoValue: When the value is not defined/nil
  • Value(Int): When the value is defined. We use the associated values with the enum case here.

The code for OptionalInt in the form of Enums is given below:

That shows that Optionals are internally Enumerations.

This brings an end to Swift Enum tutorial.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: