An enumeration is a set of related values for a user-defined data type. Keyword enum is used to define enumerated data types.
Enumeration features
enumerations in Swift are similar to struct types in C and objective C
- It is declared in a class whose value is accessed through an instance of the class
- The initial member value is defined with an enumeration initialization
- Its capabilities also extend to ensure standard protocol functionality
Grammar
enumerations are introduced into the enum keyword and within a pair of curly braces to define them:
Copy Code code as follows:
Enum Enumname {
Enumeration values are described here
}
For example, you can define an enumeration for the week as follows:
Copy Code code as follows:
Enum Daysofaweek {
Case Sunday
Case Monday
---
Case Saturday
}
Example
Copy Code code as follows:
Enum names{
Case Swift
Case Closures
}
var lang = names. Closures
Lang =. Closures
Switch Lang
{
Case. Swift:
println ("Welcome to Swift")
Case. Closures:
println ("Welcome to Closures")
Default
println ("Introduction")
}
When our above program uses playground to run, we get the following results
Enumerations in Swift do not have the default values assigned to them by C and objective C, whereas members are clearly defined by their enumeration names. Enumeration names should use uppercase letters (for example, enum Daysofaweek).
Copy Code code as follows:
var weekday = Daysofaweek.sunday
The enumerated name ' Daysofaweek ' here is assigned to a variable weekday. Sunday. It notifies the compiler that the data type belongs to subsequent enumeration members that will be assigned to that particular class in Sunday. Once the data type definition of a member is enumerated, its members can be accessed by passing a value and further calculation.
Enumerations and switch statements
the ' Switch ' statement in Swift also follows a multiple-path choice. Only one variable is accessed at a specific time based on the specified criteria. By default, a switch statement is used to capture an indeterminate situation (case).
Copy Code code as follows:
Enum climate{
Case India
Case America
Case Africa
Case Australia
}
var season = Climate.america
Season =. America
Switch season
{
Case. India:
println ("Climate are Hot")
Case. America:
println ("Climate is Cold")
Case. Africa:
println ("Climate is moderate")
Case. Australia:
println ("Climate is Rainy")
Default
println ("Climate is not predictable")
}
When our above program uses playground to run, we get the following results
The program first defines the climate as an enumeration name. Then its members such as, ' the ', ' America ', ' Africa ' and ' Australia ' Declare belong to the class ' Climate '. Now member America are assigned to a Season variable. In addition, the Switch case can find the corresponding. America, and jumps 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 does not satisfy it prints the default content ' Climate is not predictable '
Enumerations can be further divided into related and original values.
Difference between the associated value and the original value
Enumerations and associated values
Copy Code code as follows:
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).")
Default
println ("Nothing")
}
When our above program uses playground to run, we get the following results
For example, consider accessing student names and tags fixed in three accounts enumeration names are declared as student, while member names in an enumeration class are string data types, tokens are represented as MARK1, MARK2 and MARK3 data types are integers. To access the student's name or mark score:
Copy Code code as follows:
var studdetails = student.name ("Swift")
var studmarks = Student.mark (98,97,95)
Now, if the block is first executed, the switch case will print the student's name, otherwise it will print the student's fixed tag. If both of these conditions fail, the default block is executed.
Enumerations vs. original values
The original value can be a string, a character, or any integer or floating-point type. Each original value must be unique in its enumeration declaration. When an integer is used for the original value, if some of the enumeration members specified do not have any values, they are incremented automatically.
Copy Code code as follows:
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: