Swift enums
Simply speaking, an enumeration is also a data type, but this data type only contains customized specific data. It is a collection of data with common characteristics.
Swift's enumeration is similar to the structure of Objective C and C. The function of the enumeration is:
It is declared in the class and can be accessed by instantiating the class its value.
Enumerations can also define constructors (initializers) to provide an initial member value; their functionality can be extended based on the original implementation.
Can comply with protocols to provide standard functions.
Syntax
Swift uses the enum keyword to create enumerations and enclose their entire definition within a pair of curly braces:
enum enumname { // 枚举定义放在这里 }
For example, we define the following enumeration to represent 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 execution output of the above program is:
星期四
The value defined in the enumeration (such as Sunday
, Monday
, ...
and Saturday
) are the member values (or members ) of this enumeration. case
The keyword indicates that a new member value in a row will be defined.
Notice: Unlike C and Objective-C, Swift enumeration members are not assigned a default integer value when they are created. In the
DaysofaWeek
example above,Sunday
,Monday
,…
andSaturday
are not implicitly The assigned values are0
,1
,…
and6
. Instead, these enumeration members have complete values themselves, and these values are of well-definedDaysofaWeek
types. The type of
var weekDay = DaysofaWeek.THURSDAY
weekDay
can be inferred when it is initialized with one of the possible values of DaysofaWeek
. Once weekDay
is declared as a DaysofaWeek
, you can set it to the value of another DaysofaWeek
using a shortened syntax (.):
var weekDay = .THURSDAY
When the type of weekDay
is known, the enumeration name can be omitted when assigning it again. Using explicitly typed enum values makes your code more readable.
Enumerations can be divided into related values and primitive values.
The difference between related values and original values
Related values | Original values |
---|---|
Different data types | Same data type |
Instance: enum {10,0.8,"Hello"} | Instance: enum {10,35, 50} |
Values are created based on constants or variables | Pre-populated values |
The relevant value is when you create A new constant or variable based on an enumeration member is set, and its value can be different each time you do this. | The original value is always the same |
Related values
In the following example we define an enumeration type named Student, which can be a related value of Name (Int, Int, Int, Int), or Mark's A related value of type String.
import Cocoa enum Student{ case Name(String) case Mark(Int,Int,Int) } var studDetails = Student.Name("php") 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 result of the execution of the above program is:
学生的成绩是: 98,97,95。
Original value
The original value can be a string, a character, or any integer value or floating point type value. Each primitive value must be unique within its enumeration declaration.
When the original value is an integer enumeration, there is no need to explicitly assign a value to each member, Swift will automatically assign a value for you.
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 execution output of the above program is:
数字月份为: 5。