Introduction
This is part ten of Swift article series "Swift Programming - Zero to Hero". In this article, we will get a brief idea of Enumerations in Swift programming. Please read the previous articles from the links below.
- Swift Programming - Zero To Hero - Part One
- Swift Programming - Zero To Hero - Part Two
- Swift Programming - Zero To Hero - Part Three
- Swift Programming - Zero To Hero - Part Four
- Swift Programming - Zero To Hero - Part Five
- Swift Programming - Zero To Hero - Part Six
- Swift Programming - Zero To Hero - Part Seven
- Swift Programming - Zero To Hero - Part Eight
- Swift Programming - Zero To Hero - Part Nine
Enumeration
An enumeration is a user-defined datatype with certain values. It is noted by the keyword enum. By using Enumerations, users can create their own data types. In other terms, an enumeration is a data type consisting of a set of named values, called members.
According to Apple document: "An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code."
Syntax
- enum enumname {
- // enumeration values
- }
In the above syntax, enumname is the enumeration name with enum keyword. We have placed their definition within a pair of braces.
Example
- enum Gender {
- case Male
- case Female
- }
In the above example, Gender is the enumeration and Male, Female are the values or members of this enumeration. The case keyword is used to introduce new enumeration cases.
Enumeration with Type
When an enumeration is defined with type, then the members should be of the same type.
Syntax
- enum sampleEnum:DataType{
- // Members
- }
- enum Name :String{
- case FirstName
- case MiddleName
- case LastName
- }
Switch Statement using Enumerations
A switch statement is used to access one variable at a particular time based on the specified condition.
Example
- enum Programming{
- case CSharp
- case Java
- case SQL
- case HTML
- }
- var prog = Programming.CSharp
- prog= .CSharp
- switch prog{
- case .CSharp:
- print("Its a programming language developed by Microsoft")
- case .Java:
- print("Its a open source programming language")
- case .SQL:
- print("Used for Database Operations")
- case .HTML:
- print("Used to design Web Pages and Web Sites")
- }
We don’t use the default case as all the possible enum members are taken care of. Only what’s in our enums, needs to be checked for. If you do not exhaust all the enum members, then you should add the default case.
Enumerations with Associated Values and Raw Values
Associated Values
Associated values consist of different datatypes. Based on constant or variable the values are created. The values vary when declared for each time.
Raw Values
Raw values consist of the same datatypes. The values are prepopulated ones. The values for Members are same.
Conclusion
In this article, we have learned about Enumeration in Swift. Hope it was very useful. Please share your ideas and feedback in the comment section.

Sagar Pandurang KapPosted Dec 16, 2017, 12:19 AM
Great Article.Helpfull...