Swift enumeration


Release date:2023-11-30 Update date:2023-12-08 Editor:admin View counts:150

Label:

Swift enumeration

Enumeration is simply a data type, except that this data type contains only custom-specific data, which is a collection of data with common characteristics.

The enumeration of Swift is similar to the structure of Objective C and C. the function of enumeration is:

  • It is declared in the class, and its value can be accessed by instantiating the class.

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

  • Standard functionality can be provided by following the protocol.

Grammar

Used in Swift enum keywords to create enumerations and put their entiredefinitions in a pair of curly braces:

enum enumname {
   // Put the enumeration definition here
}

For example, we define the following enumeration that represents the week:

import Cocoa

// Define enumeration
enum DaysofaWeek {
    case Sunday
    case Monday
    case TUESDAY
    case WEDNESDAY
    case THURSDAY
    case FRIDAY
    case Saturday
}

var weekDay = DaysofaWeek.THURSDAY
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:
    print("Saturday")
}

The output of the above program execution is as follows:

Thursday

The values defined in the enumeration (such as Sunday , Monday , …… , and Saturday ) are the member values (or members) of this enumeration the keyword case indicates that a new member value will be defined in a row.

Note: Unlike C and Objective-C, Swift’s enumeration members are not assigned a default integer value when created. In the example of DaysofaWeek above, Sunday , Monday , …… , and Saturday do not implicitly assign values to 0 , 1 , …… , and 6 . On the contrary, these enumeration members themselves have complete values, which are of the clearly defined type of DaysofaWeek .

var weekDay = DaysofaWeek.THURSDAY

weekDay the type of can be found in the DaysofaWeek is inferred when a possible value of the Once weekDay be declared as a DaysofaWeek you can use an abbreviated syntax (.) to set it to another DaysofaWeek value:

var weekDay = .THURSDAY

When weekDay type is known, you can omit the enumeration name by assigning it again Using enumerated values of explicit types can make your code more readable.

Enumerations can be divided into related values and original values.

The difference between related value and original value

Correlation value

Original value

Different data types

Same data type

Example: enum {10cr 0.8, “Hello”}

Example: enum {10pm 35pm 50}

Values are created based on constants or variables

Pre-populated value

The related value is set when you create a new constant or variable based onenumerated members, and its value can be different each time you do so.

The original value is always the same

Correlation value

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

import Cocoa

enum Student{
    case Name(String)
    case Mark(Int,Int,Int)
}
var studDetails = Student.Name("Runoob")
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 name is: \(Mark1),\(Mark2),\(Mark3)。")
}

The output of the above program execution is as follows:

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

Original value

The original value can be a string, character, or any integer or floating point value. Each primitive value must be unique in its enumeration declaration.

When the original value is an integer enumeration, there is no need to explicitly assign values to each member, Swift will automatically assign values for you.

For example, when an integer is used as the original value, the value of theimplicit assignment increases by 1 in turn. If the first value is not assigned an initial value, 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 number of months is: \(yearMonth).")

The output of the above program execution is as follows:

The number of months is: 5.

Powered by TorCMS (https://github.com/bukun/TorCMS).