Swift Enums

A portrait painting style image of a pirate holding an iPhone.

by The Captain

on
May 14, 2023

Using Enums in Swift: A Beginner's Guide

Enums, short for enumerations, are a great tool in Swift that allow developers to define a group of related values as a single type. Enums can be used to give names to values, to group together similar types of constants, to define error states, and much more. In this tutorial, we'll go over the basics of enums in Swift and show you how to use them in your own code.

What is an Enum in Swift?

In Swift, an enum is a special type that allows you to define a group of related values as a single type. Enums are often used to represent a set of related values that can be used in your code. For example, you might define an enum to represent the different states a game can be in: "starting", "playing", or "game over". Enums can also be used to define named values for use in your code, such as the colors in a rainbow.

Creating an Enum in Swift

Creating an enum in Swift is very easy. You define an enum using the "enum" keyword, followed by the name of the enum and a set of curly braces containing the cases for the enum. Here's an example of a simple enum that defines the different types of cars:
enum CarType {
    case sedan
    case SUV
    case truck
}
In this example, we've defined an enum called "CarType" that has three cases: sedan, SUV, and truck. You can use the "case" keyword to add new cases to your enum.

Using an Enum in Swift

Once you've defined an enum in Swift, you can start using it in your code. Here's an example of how you might use our CarType enum in a function that prints out the type of car:
func printCarType(carType: CarType) {
    switch carType {
    case .sedan:
        print("This is a sedan.")
    case .SUV:
        print("This is an SUV.")
    case .truck:
        print("This is a truck.")
    }
}
In this example, we've defined a function that takes a CarType as its argument and prints out a string describing the car type. We use a switch statement to match the car type against each case in the enum. When calling this function, you would pass in one of the cases from the CarType enum:
let myCarType = CarType.sedan
printCarType(myCarType)}
This would output "This is a sedan." to the console.

Associated Values in Enums

In some cases, you might want to associate a value with each case in your enum. For example, you might define an enum for a pizza type that includes the toppings for each pizza. You can do this in Swift using associated values. Here's an example of an enum that defines different types of pizza:
enum PizzaType {
    case cheese
    case pepperoni
    case custom(toppings: [String])
}
In this example, we've defined an enum called PizzaType with three cases: cheese, pepperoni, and custom. The custom case includes an associated value of an array of strings, representing the toppings for the pizza. You can access the associated value for a case using a switch statement, like this:
let myPizza = PizzaType.custom(toppings: ["mushrooms", "peppers", "onions"])
switch myPizza {
case .cheese:
    print("This is a cheese pizza.")
case .pepperoni:
    print("This is a pepperoni pizza.")
case .custom(let toppings):
    print("This is a custom pizza with toppings: \(toppings).")
}
This would output "This is a custom pizza with toppings: ["mushrooms", "peppers", "onions"]." to the console.

Conclusion

Enums are a powerful tool in Swift that allow you to define a set of related values as a single type. In this tutorial, we've covered how to define and use enums in Swift, as well as how to use associated values to associate data with each case. With this knowledge, you can start using enums in your own Swift projects.