EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Swift Tutorial Swift enum

Swift enum

Swift enum

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Swift enum output 1

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:

Swift enum output 2

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:

Swift enum output 3

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:

output 4

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.

  1. Swift Version
  2. Swift For Loop
  3. What is Swift?
  4. How To Install Swift?
PROGRAMMING LANGUAGES Course
502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Certification Course
57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System - Design & Develop an IOT System
65+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Certification Course
19+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
SWIFT Certification Course
 70+ Hours of HD Videos
21 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more