Swift Enum Declaration: A Comprehensive Guide to Get Started

Swift Enum Declaration: A Comprehensive Guide to Get Started

Enums are a powerful tool to use in Swift programming. They allow us to create a fixed set of values that can be used as a type. By using enums, we can make our code more structured and easier to read. In this guide, we will learn about the basics of enums in Swift and how to use them in our code.

An enum is a type that consists of a set of related values. It allows us to group related values together and gives us the ability to use them as a type. This makes it easier to read and understand our code. For example, if we want to represent the days of the week, we can use an enum like this:

enum Weekdays {
    case Monday 
    case Tuesday
    case Wednesday
    case Thursday 
    case Friday
    case Saturday
    case Sunday
}

In this example, we have created an enum called Weekdays with seven values for each day of the week. We can now use this enum as a type in our code. For example, we could create a function that takes a Weekdays value as a parameter like this:

func printDay(day: Weekdays) {
    print("Today is \(day)")
}

We can now call this function and pass in a Weekdays value as a parameter. For example, we could call the function like this:

printDay(day: .Monday)

This will print out “Today is Monday”.

Enums can also have associated values. This allows us to store additional data along with the enum value. For example, if we wanted to store the temperature for each day of the week, we could add an associated value to our Weekdays enum like this:

enum Weekdays {
    case Monday(temperature: Int)
    case Tuesday(temperature: Int)
    case Wednesday(temperature: Int)
    case Thursday(temperature: Int)
    case Friday(temperature: Int)
    case Saturday(temperature: Int)
    case Sunday(temperature: Int)
}

Now when we create a Weekdays value, we can also specify the temperature for that day. For example, we could create a Weekdays value like this:

let today = Weekdays.Monday(temperature: 25)

This creates a new Weekdays value for Monday with a temperature of 25 degrees.

We can also use switch statements to handle different cases of an enum. For example, if we wanted to print out a message based on the temperature for each day, we could write a switch statement like this:

switch today {
case .Monday(let temperature):
    if temperature > 30 {
        print("It's hot today!")
    } else {
        print("It's not too hot today.")
    }
case .Tuesday(let temperature):
    if temperature > 30 {
        print("It's hot today!")
    } else {
        print("It's not too hot today.")
    }
// ...
}

This switch statement will check the temperature for each day and print out a message accordingly.

Enums can also have methods associated with them. For example, if we wanted to calculate the average temperature for the week, we could add a method to our Weekdays enum like this:

enum Weekdays {
    // ...
    
    func averageTemperature() -> Int {
        var total = 0
        var count = 0
        
        switch self {
        case .Monday(let temperature):
            total += temperature
            count += 1
        case .Tuesday(let temperature):
            total += temperature
            count += 1
        // ...
        }
        
        return total / count
    }
}

This method will calculate the average temperature for the week and return it as an Int.

Enums are a great way to structure our code and make it easier to read and understand. They also allow us to store associated values and add methods to them to perform operations on the data. By using enums, we can make our code more organized and maintainable.

Hopefully, this guide has given you an understanding of how to use enums in Swift. With a little practice, you’ll soon be able to use enums in your own code to make it more structured and easier to read.

Scroll to Top