Overview

Like other programming languages, Swift also has special features like enum, tuples, and generics. They have different use cases depending upon different conditions. In this tutorial, we will learn

What, when, and how to use enumerationstuples, and generics in Swift? So let’s get started

_This tutorial is written using _Swift 5 and Xcode 11.4.1.

1. Enumerations

According to swift documentation “An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.”

  • **Creating an enum: **You can declare an enum like this in swift
enum TimePeriod{
    case morning
    case noon
    case afternoon
    case night
}

Where the name of this enum is “TimePeriod” and it has four cases as “morning”, “noon”, “afternoon” and “night”.

We also can declare the cases in a single line like this

enum TimePeriod{
    case morning, noon, afternoon, night
}
  • **enum type: **When we define an enum itself define a new type in swift. We can define a variable as this type
var presentTime = TimePeriod.morning
print(presentTime)   // morning

“presentTime” variable is “TimePeriod” enum type and its value is “morning”

Once it defined that “presentTime” is “TimePeriod” type we can set others value of enum using “.” operation

presentTime = .noon
presentTime = .afternoon
presentTime = .night
  • **Matching enum value: **Swift switch statement is a simple and straight forward solution for matching the enum value.
switch presentTime {

case .morning:
    print("Foggy morning")
case .noon:
    print("Sunny noon")
case .afternoon:
    print("Cloudy afternon")
case .night:
    print("Rainy night")
}
  • Iterate enum cases: To iterate enum cases we have to declare enum as“CaseIterable”. In our case, we can declare our “TimePeriod” enum like this.
enum TimePeriod: CaseIterable{
    case morning
    case noon
    case afternoon
    case night
}

Now we can get all cases and iterate as follow.

let numberOfChoices = TimePeriod.allCases.count
print("\(numberOfChoices) timeperiod available")

for timeperiod in TimePeriod.allCases{
    print(timeperiod)
}
  • **Raw Value: **We can provide value for every enum case. These values are known as Raw Value. It can provide both implicitly and explicitly. Before that, we have to provide the type of raw value after enum name
enum Institute: Int{

case school
case colleague
case university
}

enum Institute will have an integer type of raw value for every case. It can be any type in swift. In this case value for school is 0, colleague 1, and university as 2.

Raw values of every case can be check like this

print(Institute.school.rawValue) //0
print(Institute.colleague.rawValue) //1
print(Institute.university.rawValue) //2

If we want we also can provide explicitly raw value for every case anything we want.

enum Institute: Int{

case school = 10
case colleague = 12
case university = 14
}

We also can explicitly provide raw value for the first case. In this case, swift will provide raw values for other cases implicitly

enum Institute: Int{

case school = 4
case colleague
case university
}

Here raw value for school is explicitly defined as 4. Swift will provide raw value for colleague as 5 and university as 6 implicitly.

We can search a case from enum using raw value. If any case present with that corresponding raw value swift will return that case name else it will return nil.

var instituteType = Institute(rawValue: 9)

if let value = instituteType2 {
print(value) // nil because we haven't any case with rawvalue 9
}

Lastly, we will see an example of string raw value of cases

enum MoveDirection : String {

case forward = "you moved forward"
    case back = "you moved backwards"
    case left 
    case right = "you moved to the right"
}

#swift #generics #enumeration #ios #tuples

Enumerations, Tuples, and Generics in Swift
1.15 GEEK