Swift Enumerations
Enumerations are, simply put, also a data type, except that this data type only contains specific user-defined data; it is a collection of data with common characteristics.
Swift enumerations are similar to the constructs in Objective-C and C. The features of enumerations are:
It is declared in a class, and its value can be accessed by instantiating the class.
Enumerations can also define initializers to provide an initial member value; you can extend their functionality based on the original implementation.
They can conform to protocols to provide standard functionality.
Syntax
In Swift, the enum keyword is used to create enumerations, and their entire definition is placed inside a pair of curly braces:
enum enumname {
// 枚举定义放在这里
}
For example, we define the following enumeration representing the days of 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:
Thursday
The values defined in an enumeration (such asSunday,Monday,……andSaturday) are this enumeration'smember values(ormembers)。caseThe keyword indicates that a new line of member values will be defined.
Note: Unlike C and Objective-C, Swift's enumeration members are not assigned a default integer value when created. In the above
DaysofaWeekexample,Sunday,Monday,……andSaturdaywill not be implicitly assigned as0,1,……and6. Instead, these enumeration members themselves have complete values, which are already well-definedDaysofaWeektype.
var weekDay = DaysofaWeek.THURSDAY
weekDayThe type can be inferred when it isDaysofaWeekinitialized with one of its possible values. OnceweekDaydeclared as aDaysofaWeek, you can use a shorthand syntax (.) to set it to anotherDaysofaWeekvalue:
var weekDay = .THURSDAY
WhenweekDayWhen the type is known, you can omit the enumeration name when assigning a value to it again. Using explicitly typed enumeration values can make the code more readable.
Enumerations can be divided into associated values and raw values.
Difference Between Associated Values and Raw Values
| Associated Values | Raw Values |
|---|---|
| Different data types | Same data type |
| Example: enum {10,0.8,"Hello"} | Example: enum {10,35,50} |
| Values are created based on constants or variables | Pre-filled values |
| Associated values are set only when you create a new constant or variable based on an enumeration member, and each time you do so, its value can be different. | Raw values are always the same |
Associated Values
In the following example, we define an enumeration type named Student, which can be a string (String) for Name, or an associated value (Int, Int, Int) for Mark.
import Cocoa
enum Student{
case Name(String)
case Mark(Int,Int,Int)
}
var studDetails = Student.Name("Example")
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。
Raw Values
Raw values can be strings, characters, or any integer or floating-point value. Each raw value must be unique within its enumeration declaration.
For enumerations with integer raw values, you don't need to explicitly assign a value to each member; Swift will automatically assign values for you.
For example, when integers are used as raw values, the implicitly assigned values increase by 1 in sequence. If the first value is not assigned an initial value, it will automatically be 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。Other Extensions