Introduction
In this article, we will learn another feature of Swift programming language, enumeration. In other languages you also see the concept of enumeration but in swift there is a little bit of a difference in Swift language enumeration.
What is Enumeration?
- An enumeration is a list which contains a list of related values that define common type
- Enumeration provides an alternative to old-fashioned integer values
- With the help of enumeration, you can restrict the values, store state and encapsulated values.
- An enumeration can have methods, properties and protocol declaration.
- Enumeration shares features with structures and classes
Enumeration Syntax & Declaration Ways
enum name {
// case A
// case B
}
case keyword is mandatory in enumeration.
Fortunately, Swift gives two ways to declare the enumeration and you have a choice how you declare an enumeration. Here are the declaration ways. In this example we simply declarare the months of enums.
Code
enum oneWayMonths
{
case January
case February
case March
case April
case May
case June
}
enum secondWayMonths
{
case January,February,March,April,May,June
}
How Do We Use Enumeration in your Code?
Now we take previous code and implement it in your code by using two ways,
In this example, we declare a new variable name as “monthnumber” after that it gives a colon and then enumeration name --after that in the second line we declare the enumeration directly and use its type in the third line. The variable which is declared in the first line, we assign it enumeration, and then use its type.
Enumeration as a Parameter of Function
In this example, we simple declare enum named as “secondWayMonth” and then simply I am declaring months using case keyword; this keyword is mandatory for enumeration declaration.
Now we make a function named “monthfunc” and then we pass the enumeration as a function parameter and then we create switch statement and use the enumeration and our goal is that when we call month, January, it prints the month number so in this way we print one line in each month. After that we call a function and in round brackets we first declare enumeration then give the month name, then it prints the month number which you would see in the right side and in front of case.
Enumeration as A Raw Value
Swift enum values by default are not backed by integers; that means that January, which is declared at enum, has itself the value. But you can associate a raw value with each enumeration. Here is the example which clears your concept:
In this example, we simply declare the enum with raw values but when you declare with raw value we are making mandatory which type of raw value it is -- integer, string etc.
After declaration of enumeration with raw values we simply again make a function and then pass enumeration as a parameter of function and return the raw value of each month then we call the function, and February, which is called through enumeration, gives its raw value
Now this is the end of the enumeration topic; hopefully you understand well and if you're facing any difficulty or confusion you can ask me at any time.