An enumeration is a set of related values ​​of a user-defined data type.The keyword enum is used to define the enumerated data type.

Enumeration functionEnumerations in swift are also similar to struct types in c and objective c

It is declared in a class,Its value is accessed through an instance of this class Initial member values ​​are defined with enum initialization Its functions are also extended to ensure standard protocol functions

grammarThe enumeration introduces the enum keyword and defines them within a pair of braces:

enum enumname {
 //enumeration values ​​are described here

For example, you can define an enumeration for the week as follows:

enum daysofaweek {
 case sunday
 case monday
 case saturday


enum names {
 case swift
 case closures
var lang=names.closures
switch lang
 case .swift:
 println ("welcome to swift")
 case .closures:
 println ("welcome to closures")
 println ("introduction")

When our above program is run using playground, we get the following result

welcome to closures

Enums in swift don't assign default values ​​for their members like c and objective c,Instead, members are clearly defined by their enumerated names.Enumeration names should use uppercase letters (for example:enum daysofaweek).

var weekday=daysofaweek.sunday

The enumeration name "daysofaweek" described here is assigned to a variable weekday.sunday. It informs the compiler that the data type belongs to a subsequent enum member that will be assigned to that particular class on Sunday.Once the data type of the enumeration member is defined,Its members can be passed values,And make further calculations to visit.

Enumeration and switch statementsThe "switch" statement in swift also follows multiple choices. Only one variable is accessed at a specific time based on specified conditions.by default,The switch statement is used to catch uncertain cases.

enum climate {
 case india
 case america
 case africa
 case australia
var season=climate.america
switch season
 case .india:
 println ("climate is hot")
 case .america:
 println ("climate is cold")
 case .africa:
 println ("climate is moderate")
 case .australia:
 println ("climate is rainy")
 println ("climate is not predictable")

When our above program is run using playground, we get the following result

climte is cold

The program first defines climate as the enumeration name.Then its members such as, "china", "america", "africa" ​​and "australia" are declared to belong to the class "climate". The member america is now assigned to a season variable. In addition, the switch case can find the value of .america and jump to that particular statement. The output will appear as "climate is cold". Similarly, all members can be accessed through a switch statement. When the condition is not met it prints the default content "climate is not predictable"

Enumerations can be further divided into related and raw values.

Difference between associated and original values

Enumerations and associated values

enum student {
 case name (string)
 case mark (int, int, int)
var studdetails=student.name ("swift")
var studmarks=student.mark (98,97,95)
switch studmarks {
 case .name (let studname):
 println ("student name is:\ (studname).")
 case .mark (let mark1, let mark2, let mark3):
 println ("student marks are:\ (mark1), \ (mark2), \ (mark3).")
 println ("nothing")

When our above program is run using playground, we get the following result


For example, consider the names and tags of visiting students fixed in three subjects. The enumeration name is declared as student and the member names in the enumeration class are all string data types.Marks are represented as mark1, and mark2 and mark3 are integers.To access student names or mark scores:

var studdetails=student.name ("swift")
var studmarks=student.mark (98,97,95)

Now, if the block executes at the beginning,switch case will print the student name,Otherwise it will print student-fixed marks. If both conditions fail,The default block will be executed.

Enumeration and primitive valuesThe original value can be a string.Character, or any integer or floating point type.Each primitive value must be unique in its enumeration declaration.When integers are used for raw values,If some of the specified enumeration members have no value,They are automatically incremented.

enum month:int {
 case january=1, february, march, april, may, june, july, august, september, october, november, december
let yearmonth=month.may.rawvalue
println ("value of the month is:\ (yearmonth).")

When we run the above program using playground, we get the following results:

value of the month is:5.
  • Previous How to implement jQuery multi-condition filtering
  • Next Easily master Linux shutdown and restart commands