Updated April 5, 2023
Introduction to Swift enum
The swift enum is a special data type to create a set of constant values, variables, and properties. The enum is created a user-defined interface using the enum keyword and describes the required variable and its value as per users’ choice. The enum is a collection of the constant elements used in the swift code repeatedly. The enum is the user-defined data type and creates a set of constant elements to use in the swift condition statement. The enum is the enumerated data type that consists of the set of the constant variable and its value. The enum is called also swift enumeration.
Syntax:
The swift enum syntax is below.
enum Enum_Name {
// defined datatype and its value here…
}
Description:
- The enumeration uses the “enum” keyword to create the user-defined interface.
- The Enum_Name is the name of the enum and works as the class name.
- The enum elements and value are placed inside of the enum.
- The enum value is accessed in the swift code through the instance of the enum class.
How does the enum function work in Swift?
The swift installation for the enum programming.
- The Ios and OS x users have installed Xcode IDE but the user must have an account on the apple developer website.
- Users enter the playground name and select the platform name in the Xcode IDE.
- Users can get the playground window and start the coding.
- The other operating system users can use online options for swift programming.
- The user can use the https://iswift.org/playground website for swift coding.
The enum syntax is used to create a user-defined data type and its value.
enum Oceans
{
case Pacific
case Indian
case Atlantic
case Southern
case Arctic
}
The enum value is accessed in the swift code through the instance of the enum class.
var oceans = Oceans.Indian;
oceans = .Indian;
The value is used in the swift code as per the user’s requirement.
switch oceans {
case .Pacific:
print("This is pacific ocean.")
case .Indian:
print("This is Indian ocean.")
case .Atlantic:
print("This is Atlantic ocean.")
case .Southern:
print("This is the southern ocean.")
case .Arctic:
print("This is the arctic ocean.")
default:
print("There is no ocean.")
}
Combine the step of the working procedure of the enum.
enum Oceans
{
case Pacific
case Indian
}
var oceans = Oceans.Indian;
oceans = .Indian;
switch oceans {
case .Pacific:
print(" This is pacific ocean. ")
case .Indian:
print(" This is Indian ocean. ")
default:
print(" There is no ocean. ")
}
Description:
- It is declared the value of the oceans as a constant element.
- The instance of that class used “Indian” as a member of the enum.
- The variable select .indian as a constant value in the enum.
- The switch cases return the value of “case . Indian”.
Examples
Here are the following examples mentioned below.
Example #1
The basic enum with the same instance and variable example and output are below.
Code:
enum Oceans
{
case Pacific
case Indian
case Atlantic
case Southern
case Arctic
}
var oceans = Oceans.Indian;
oceans = .Indian;
switch oceans {
case .Pacific:
print("This is pacific ocean.")
case .Indian:
print("This is Indian ocean.")
case .Atlantic:
print("This is Atlantic ocean.")
case .Southern:
print("This is the southern ocean.")
case .Arctic:
print("This is the arctic ocean.")
default:
print("There is no ocean.")
}
Output:
Description:
- The enum used Indian as an associate member name with enum class name.
- The switch case used oceans = .Indian as a constant value of the enum.
Example #2
The basic swift enum with different instances and variable example and output are below.
Code:
enum Oceans
{
case Pacific
case Indian
case Atlantic
case Southern
case Arctic
}
var oceans = Oceans.Indian;
oceans = .Southern;
switch oceans {
case .Pacific:
print("This is pacific ocean.")
case .Indian:
print("This is Indian ocean.")
case .Atlantic:
print("This is Atlantic ocean.")
case .Southern:
print("This is the southern ocean.")
case .Arctic:
print("This is the arctic ocean.")
default:
print("There is no ocean.")
}
Output:
Description:
- The enum used Indian as an associate member name with enum class name.
- The switch case used oceans = .Pacific as a constant value of the enum.
Example #3
The basic enum with associated values example and output are below.
Code:
enum StudentInfo
{
case Student_name(String)
case Student_addr (String, String, Int)
case Student_mark (Int, Int)
}
var student_name = StudentInfo.Student_name(" Swift Enum ");
var student_addr = StudentInfo.Student_addr(" st. road", " nyk ", 453);
var student_mark = StudentInfo.Student_mark(25, 28);
switch student_addr {
case .Student_name(let st_name):
print("This is studet name: \(st_name). ")
case .Student_addr(let std_street, let std_city, let std_pin):
print("This is student address: \(std_street), \(std_city), \(std_pin). ")
case .Student_mark(let st_mar1, let st_mar2):
print("This is student mark: \(st_mar1), \(st_mar2).")
}
Output:
Description:
- The enum with associated values can use the same or different data types.
- The enum with associated values based on variable or data types.
Example #4
The basic enum with raw values example and output are below.
Code:
enum WeekDays: Int
{
case monday = 1
case tuesday
case wednesday
case thursday
case friday
case saturday
case sunday
}
let weekday = WeekDays.monday.rawValue
let weekday1 = WeekDays.saturday.rawValue
print("the swift enum with initialize Int raw value. ")
print("the Int raw value of monday is : \(weekday)")
print("the Int raw value of saturday is : \(weekday1)")
enum WeekDays1: String
{
case monday = "a"
case tuesday = "b"
case wednesday = "c"
case thursday = "d"
case friday = "e"
case saturday = "f"
case sunday
}
let weekday2 = WeekDays1.monday.rawValue
let weekday3 = WeekDays1.saturday.rawValue
let weekday4 = WeekDays1.sunday.rawValue
print("the swift enum with initialize string raw value. ")
print("the String raw value of monday is : \(weekday2)")
print("the String raw value of saturday is : \(weekday3)")
print("the String raw value of sunday is : \(weekday4)")
Output:
Description:
- The enum with associated values can use the same datatypes.
- The enum with associated values based on predefined values.
Conclusion
- The enum is a set of similar and constant values or elements.
- The enum helps programmers to reduce the coding and use the same code often.
- The enum is user-defined output to understand the actual value of the condition statement.
Recommended Articles
We hope that this EDUCBA information on “Swift enum” was beneficial to you. You can view EDUCBA’s recommended articles for more information.