Swift Programming - Zero To Hero - Part Ten

Introduction 
 
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 
  1. enum enumname {  
  2.    // enumeration values   
  3. }  
In the above syntax, enumname is the enumeration name with enum keyword. We have placed their definition within a pair of braces.
 
Example  
  1. enum Gender {  
  2. case Male  
  3. case Female  
  4. }  
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 
  1. enum sampleEnum:DataType{  
  2. // Members  
  3. }  
 Example
  1. enum Name :String{  
  2. case FirstName  
  3. case MiddleName  
  4. case LastName  
  5. }  
 In the above example, Name is enumeration that is strongly typed with String type. FirstName, MiddleName, LastName are the members of this enumeration Name with String datatype.
 
Switch Statement using Enumerations 
 
A switch statement is used to access one variable at a particular time based on the specified condition.
 
Example 
  1. enum Programming{  
  2.    case CSharp  
  3.    case Java  
  4.    case SQL  
  5.    case HTML  
  6. }  
  7.   
  8. var prog = Programming.CSharp  
  9. prog= .CSharp  
  10. switch prog{  
  11.    case .CSharp:  
  12.       print("Its a programming language developed by Microsoft")  
  13.    case .Java:  
  14.       print("Its a open source programming language")  
  15.    case .SQL:  
  16.       print("Used for Database Operations")  
  17.    case .HTML:  
  18.       print("Used to design Web Pages and Web Sites")  
  19.      
  20. }  
 In the above-given example, Enumeration name is Programming and CSharp, Java, SQL, and HTML are its members. Then, I have created a variable prog and CSharp Enum value is assigned to it. Prog value is the switch case value. Now, when we run the program on the playground, we will get a result as "It's a programming language developed by Microsoft".
 
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.  

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now