Latest web development tutorials

Swift enumeration

Enumeration is simply a data type, but this is the only data type that contains a custom specific data, which is a set of common characteristics of the data collection.

Swift enumeration structure similar to Objective C and C, enumeration functions:

  • It is declared in the class, the class can access its value by instantiating.

  • Enum can also define a constructor (initializers) to provide an initial member value; can extend their functionality in the original implementation basis.

  • You can observe protocol (protocols) to provide a standard feature.


Swift enum keyword is used to create and to enumerate their entire definitions placed in a pair of braces:

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

For example, we define the following day of the week enumeration:

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:
case .Monday:
case .TUESDAY:
case .FRIDAY:
case .Saturday:

The above program execution output is:


Value (as defined in the enumeration of Sunday , Monday , …… and Saturday ) are the members of the enumeration values (or member). case Keywords members represent a new line of value will be defined.

Note: C and Objective-C and different enumeration member Swift will not be given a default integer value is being created. In the above DaysofaWeek example, Sunday , Monday , …… and Saturday will not implicitly assigned the value 0 , 1 , …… and 6 . Instead, these members themselves have a complete enumeration values that are already clearly defined DaysofaWeek type.

var weekDay = DaysofaWeek.THURSDAY 

weekDay type it may be DaysofaWeek inferred when a value may be initialized. Once weekDay is declared as a DaysofaWeek , you can use an abbreviated syntax to set it to another (.) DaysofaWeek value:

var weekDay = .THURSDAY 

When weekDay when the type is known, its assignment may be omitted again enumeration name. Use explicit type enumeration values ​​makes the code more readable.

It can be divided into enumeration value associated with the original value.

The difference between the correlation value and the original value

The correlation value Original value
Different types of data The same data type
Examples: enum {10,0.8, "Hello"} Examples: enum {10,35,50}
Create a value based on constant or variable Value pre-populated
Correlation value when you create a new membership-based enumeration constant or variable will be set, and every time when you do have time, its value may be different. The original value is always the same

The correlation value

The following example, we define a named Student of the enumerated type, it can be an associated value Name (Int, Int, Int, Int), or Mark a string type (String) correlation value.

import Cocoa

enum Student{
    case Name(String)
    case Mark(Int,Int,Int)
var studDetails = Student.Name("w3big")
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 above program execution output is:

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

Original value

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

When the original value is an integer enumeration, without an explicit assignment for each member, Swift will automatically assign you.

For example, when using an integer value as the original, the implicit value assigned sequentially incremented by one. If the first value is not assigned the initial value will be automatically set to zero.

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 above program execution output is:

数字月份为: 5。