Swift enumerity

Enumerity is simply a data type, but it contains only a specific set of custom data, a collection of data with common characteristics.

Swift's enumerity is similar to the structure of Objective C and C, and the enumerity features are:

  • It declares that in a class, its value can be accessed by instantiated by the class.

  • Enumerations can also define constructors to provide an initial member value;

  • Protocols can be complied with to provide standard functionality.

Grammar

Swift uses enum keywords to create enum keywords and places their entire definition in a pair of braces:

enum enumname {
   // 枚举定义放在这里
}

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

import Cocoa

// 定义枚举
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("星期天")
case .Monday:
    print("星期一")
case .TUESDAY:
    print("星期二")
case .WEDNESDAY:
    print("星期三")
case .THURSDAY:
    print("星期四")
case .FRIDAY:
    print("星期五")
case .Saturday:
    print("星期六")
}

The output of the above program execution is:

星期四

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

Note: Different from C and Objective-C, Swift's enumeration members are not given a default integer value when they are created. I n the DaysofaWeek above, Sunday Monday …… a nd Saturday are not implicitly assigned 0 1 …… a nd 6 Instead, these enumeration members have complete values of their own, which are well-defined DaysofaWeek

var weekDay = DaysofaWeek.THURSDAY 

weekDay weekDay can be inferred when it is initialized by a possible value of DaysofaWeek Once weekDay as a DaysofaWeek can set it to another DaysofaWeek value using an DaysofaWeek

var weekDay = .THURSDAY 

When weekDay is known, assigning it again can omit enumeration. Using enumeration values of explicit types can make your code more readable.

Enumeration can be divided into related values and original values.

The difference between the relevant value and the original value

The relevant value The original value
Different data types The same data type
Example: enum s10,0.8, "Hello" Example: enum s10,35,50
Values are created based on constants or variables Pre-populated values
Correlation values are set when you create a new constant or variable based on enumeration members, and each time you do so, its value can be different. The original values are always the same

The relevant value

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

import Cocoa

enum Student{
    case Name(String)
    case Mark(Int,Int,Int)
}
var studDetails = Student.Name("W3CSchool")
var studMarks = Student.Mark(98,97,95)
switch studMarks {
case .Name(let studName):
    print("学生的名字是: \(studName)。")
case .Mark(let Mark1, let Mark2, let Mark3):
    print("学生的成绩是: \(Mark1),\(Mark2),\(Mark3)。")
}

The output of the above program execution is:

学生的成绩是: 98,97,95。

The original value

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

When the original value is enumeration of an integer, there is no need to explicitly assign a value to each member, and Swift automatically assigns you a value.

For example, when an integer is used as the original value, the value of the implicit 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("数字月份为: \(yearMonth)。")

The output of the above program execution is:

数字月份为: 5。