Enumeration in Swift : explanation with examples

Enumeration in Swift 4 :

Enumeration is actually used to define a common types of group of data.For example, following is a enumeration with seven different values or cases :

enum Day{
    case sunday
    case monday
    case tuesday
    case wednesday
    case thrusday
    case friday
    case satday
}

Now, we can use one switch case to match a specific enumeration value like :

enum Day{
    case sunday
    case monday
    case tuesday
    case wednesday
    case thrusday
    case friday
    case satday
}

let currentDay : Day = .thrusday

switch currentDay{
case .sunday:
    print("Today is Sunday")
case .monday:
    print("Today is Monday")
case .tuesday:
    print("Today is Tuesday")
case .wednesday:
    print("Today is Wednesday")
case .thrusday:
    print("Today is Thrusday")
case .friday:
    print("Today is Friday")
case .satday:
    print("Today is Saturday")
}

It will print Today is Thrusday.

This is a basic example of enumeration in swift. I will publish one different post with some complex examples in future.

Leave a Reply