English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Swift Enums

In simple terms, an enumeration is also a data type, but this data type only contains custom specific data, it is a collection of a set of data with common characteristics.

Swift's enumerations are similar to Objective C and C structures, with enumeration functions being:

  • It is declared in a class and its values can be accessed by instantiating the class.

  • Enumerations can also define initializers (initializers) to provide an initial member value; they can extend their functionality based on the original implementation.

  • They can conform to protocols (protocols) to provide standard functionality.

Syntax

In Swift, the enum keyword is used to create enumerations and their entire definition is placed within a pair of curly braces:

enum enumname {
   // Enumeration definition is placed here
}

For example, we define the following enumeration to represent the week:

import Cocoa
// Define enumeration
enum DaysOfWeek {
    case .Sunday
    case .Monday
    case .TUESDAY
    case .WEDNESDAY
    case .THURSDAY
    case .FRIDAY
    case .Saturday
}
type.
weekDay = .THURSDAY
switch weekDay
{
case .Sunday:
    print("Sunday")
case .Monday:
    print("Monday")
case .TUESDAY:
    print("Tuesday")
case .WEDNESDAY:
    print("Wednesday")
case .THURSDAY:
    print("Thursday")
case .FRIDAY:
    print("Friday")
case .Saturday:
    case .Saturday:
}

The output result of the above program is:

print("Saturday")

Thursday instance,will not be implicitly assigned a default integer value.Sundaywill not be implicitly assigned a default integer value.……Mondaydefined values in this enumeration(such as)are themember values(orMembers)。case

keywords indicate that a new member value will be defined on a new line. Note:-and C and ObjectiveDays of a WeekC is different, Swift's enumeration members are not assigned a default integer value when they are created. In the aboveinstance,will not be implicitly assigned a default integer value.Sundaywill not be implicitly assigned a default integer value.……MondaySaturday0will not be implicitly assigned a default integer value.1will not be implicitly assigned a default integer value.……6andDays of a Week. Conversely, these enumeration members themselves have complete values, which are already well-defined

type.

weekDayvar weekDay = DaysofaWeek.THURSDAYDays of a Weekpossible value when it is initialized. OnceweekDaythat is declared as aDays of a Week,and you can use an abbreviated syntax (.) to set it to anotherDays of a Weekhas the value:

var weekDay = .THURSDAY

whenweekDayWhen the type of the enumeration is known, you can omit the enumeration name when assigning it again. Using explicitly typed enumeration values can make the code more readable.

Enumerations can be divided into related values and original values.

Difference between Related Values and Original Values

Related ValuesRaw Value
Different Data TypesSame Data Type
Example: enum {10,0.8,"Hello"Example: enum {10,35,50}
The creation of values is based on constants or variablesPre-assigned Values
Related values are set when you create a new constant or variable based on an enumeration member, and each time you do so, its value can be different.The original values are always the same

Related Values

In the following example, we define an enumeration named Student, which can be a string (String) of Name or a related value (Int,Int,Int) of Mark.

import Cocoa
enum Student{
    case Name(String)
    case Mark(Int,Int,Int)
}
var studDetails = Student.Name("w3codebox")
var studMarks = Student.Mark(98,97,95)
switch studMarks {
case .Name(let studName):
    print("The student's name is: (studName)。")
case .Mark(let Mark1, let Mark2, let Mark3):
    print("The student's score is: (Mark1),(Mark2),(Mark3)。
}

The output result of the above program is:

The student's grades are: 98,97,95.

Raw Value

Raw values can be strings, characters, or any integer or floating-point value. Each raw value must be unique in its enum declaration.

In enums with integer raw values, there is no need to explicitly assign values to each member. Swift will automatically assign them for you.

For example, when using integers as raw values, the implicit assignment values increase sequentially1. If the first value is not initialized, it will be automatically set to 0.

import Cocoa
enum Month: Int {
    case January = 1, February, March, April, May, June, July, August, September, October, November, December
}
let yearMonth = Month.May.rawValue
print("The numeric month is: \(yearMonth).")

The output result of the above program is:

The numeric month is: 5.