David's Blog

Swift Enums A Powerful and Versatile Tool for Developers

By David Li on 2024-05-27T17:08:22.000Z

Swift Enums: A Powerful and Versatile Tool for Developers

Swift is a modern, type-safe programming language that puts great emphasis on clarity and safety. One of the many powerful features Swift offers is enumerations, commonly abbreviated as enums. Enums are a convenient way to represent a group of related values, allowing you to work with those values in a type-safe manner.

In this article, we’ll dive into the power of Swift enums, explore their various features, and learn how they can be used to make your code more concise, expressive, and safe.

Enums in Swift: Basic Syntax

Enumerations are defined using the enum keyword. The basic syntax for defining an enumeration is as follows:

enum EnumerationName {
    case value1
    case value2
    case value3
    // ...

The enumeration name should be in PascalCase, while the cases should be in camelCase. For example:

enum CompassDirection {
    case north
    case south
    case east
    case west

To use an enumeration value, you can reference it by its type and its case name, separated by a dot:

let direction: CompassDirection = .north

Associated Values

One of the unique features of Swift enums is their ability to store associated values. Associated values allow you to attach additional information to each case, making enums even more powerful and flexible.

Here’s an example of an enum with associated values:

enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)

In this example, the Barcode enum has two cases: upc and qrCode. The upc case has four associated Int values, while the qrCode case has one associated String value.

To create an instance of an enum with associated values, you can provide the values in parentheses after the case name:

let productBarcode = Barcode.upc(8, 85909, 51226, 3)
let qrCode = Barcode.qrCode("ABCDEFGHIJKLMNOP")

Pattern Matching with Enums

Swift provides powerful pattern matching capabilities that work seamlessly with enums. One common way to use pattern matching with enums is with the switch statement.

Here’s an example of using a switch statement to handle the different cases of the Barcode enum:

func printBarcodeInfo(_ barcode: Barcode) {
    switch barcode {
    case .upc(let numberSystem, let manufacturer, let product, let check):
        print("UPC: \(numberSystem), \(manufacturer), \(product), \(check)")
    case .qrCode(let code):
        print("QR code: \(code)")

printBarcodeInfo(productBarcode) // Output: UPC: 8, 85909, 51226, 3
printBarcodeInfo(qrCode) // Output: QR code: ABCDEFGHIJKLMNOP

In the switch statement, we use the let keyword to extract the associated values for each case.

Enums with Raw Values

Swift enums can also have raw values. Raw values are automatically assigned to each case, and they can be of any string, character, integer, or floating-point type. To define an enum with raw values, specify the raw value type after the enum name, and assign a raw value to each case.

Here’s an example of an enum with raw integer values:

enum Planet: Int {
    case mercury = 1
    case venus = 2
    case earth = 3
    case mars = 4
    case jupiter = 5
    case saturn = 6
    case uranus = 7
    case neptune = 8

let planetRawValue = Planet.earth.rawValue // planetRawValue is 3

You can also create an enum instance from a raw value using the init?(rawValue:) initializer:

if let planet = Planet(rawValue: 3) {
    print("Planet with raw value 3 is \(planet).") // Output: Planet with raw value 3 is earth.
} else {
    print("No planet with raw value 3.")


Swift enums are an incredibly powerful and versatile feature that makes your code more expressive, concise, and safe. They provide a type-safe way to work with a group of related values and offer features like associated values, powerful pattern matching, and raw values.

By leveraging the power of Swift enums, you can write cleaner, more maintainable, and more expressive code that is easier to understand and work with.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-06-14.