Swift Enumerations

Placement-ready Online Courses: Your Passport to Excellence - Start Now

Enumerations are a custom data type. It makes a group of related values as a single data type. It provides type safety for various cases or states. It is also known as enums.

In this article, we’ll learn about its declaration, usage and concepts related to it. We understand this with the help of relevant examples.

Declaration and Usage

We define an enum with an enum keyword followed by an enum’s name. We enclose the different cases of the enum within curly braces {}. We define the cases with the case keyword followed by the name of the case. The following syntax and example will explain this better.

The syntax of using enums is

enum enumName{
    case caseName1
    case caseName2
    ...
    case caseNameN
}

An example of an enum is as follows

enum enumExample{
    case Sunny
    case Windy
    case Rainy
}

We can access each of the cases in the following way

enumName.caseName

Enums with Switch Statement

The switch statements involve the usage of cases. Enumerations allow a multi-way selection of cases in a switch statement.

enum enumExample{
    case DataFlair
    case Swift
    case SwitchStatements
    case Enumerations
}

var example = enumExample.DataFlair
switch(example){
    case .DataFlair:
        print("DataFlair selected")
    case .Swift:
        print("Swift selected")
    case .SwitchStatements:
        print("Switch Statements selected")
    case .Enumerations:
        print("Enumerations selected")
}

Output:

DataFlair selected

Enums with Raw Values

We can assign constant values to each of the cases in an enum. These values are called Raw values. These values can be of any data type.

enum enumExample: Int{
    case thisYear = 2023
    case lastYear = 2022
    case nextYear = 2024
}

var year = enumExample.thisYear

print(year.rawValue)

Output:

2023

Enums with Associated Values

We can have values associated with each case of enums. For example,

enum enumExample{
    case name(String)
    case date(Int, Int)
}

var name = enumExample.name("DataFlair")
var date = enumExample.date(11, 2023)

print(name, date)

Output:

name(“DataFlair”) date(11, 2023)

A practical example of using enums with associated value is maintaining an inventory. These inventories identify every product with a unique ID. These IDs can be presented using QR codes or UPC codes. The QR code usually depicts a unique string. The UPC code represents a tuple of integer values.

enum productID{
    case productUPCCode(Int, Int, Int, Int)
    case productQRCode(String)
}

var productWithQR = productID.productQRCode("SCANTHEQRCODE")
var productWithUPC = productID.productUPCCode(6, 10762, 56932, 7)

print(productWithQR)
print(productWithUPC)

Output:

productQRCode(“SCANTHEQRCODE”)
productUPCCode(6, 10762, 56932, 7)

Distinction between Raw and Associated Values

Raw ValueAssociated Value
Constant values assigned to casesValues are assigned based on variable-defined
All the raw values must be of the same typeThe associated values can be of different types
Value remains the same throughputValue varies every time defined
Eg: enum{2022, 2023}Eg: enum{“DataFlair”, 2023}

Conclusion

Enumerations make a group of related values as a single data type. This article covers enums and concepts related to it. We have seen how to use enums in switch statements. We also learned about raw values and associated values in enums.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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