In swift, enumeration is a data type which consists a set of related values and it allow us to use those defined values in our code in a type-safe manner.
The values whatever we defined in swift enumeration can be a string or a character or a value of integer or float. We can create different type of enumeration examples in swift like collection of colors, cards, animals and etc.
The swift enumeration is powerful when we compare with other programming languages such as Objective-C and etc. because enumeration shares their functionalities with structures and class. An enumeration in swift is a user-defined so it contains methods, protocol, and properties.
Generally, in swift we use enum
keyword to define a new enumeration followed by its name and the values in enumeration can be defined with a case statement.
Following is the syntax of using enumeration in a swift programming language.
enum enumname{
//Case A
//Case B
//Case C
---------
//Case N
}
If you observe above swift enumeration syntax we used enum keyword followed by its name and the values in enumeration defined using case statement.
Now we will see how to use enumerations in a swift programming language with examples.
Following is the example of defining and using enumerations in a swift programming language.
enum Zoo {
case Tiger
case Lion
case Monkey
case Bear
case Elephant
}
var animal = Zoo.Tiger
if animal == .Tiger {
print("It’s a Tiger")
}
If you observe the above enumeration example we defined enum (“Zoo”) with a set of related values using case keyword and we are accessing and comparing enumeration values based on our requirements.
If we are known about enumeration type then we can drop the type and use dot syntax to compare with enumeration values.
When we run the above example in swift playground we will get a result like as shown below
In swift, we can define enumeration in another way like multiple case statements in a single line, separated by commas.
Following is the different approach of defining enumeration values.
enum Zoo {
case Tiger, Lion, Monkey, Bear, Elephant
}
var animal = Zoo.Tiger
if animal == .Tiger {
print("It’s a Tiger")
}
If you observe above example we defined enumeration values in a single line which separated by commas.
When we run above example in swift playground we will get a result like as shown below
This is how we can define or declare enumeration in swift programming language based on our requirements.
Generally in swift enumeration we use case keyword to define enumeration values and swift allow us to declare individual values depend upon which pattern we will follow in our code.
In swift, we can use enumeration with a Switch statement to match individual enumeration values.
Following is the example of using swift enumeration with a Switch statement.
enum Zoo {
case Tiger
case Lion
case Monkey
case Bear
case Elephant
}
var animal = Zoo.Lion
switch animal {
case .Tiger:
print("It’s a Tiger")
case .Lion:
print("It’s a Lion")
case .Monkey:
print("It’s a Monkey")
case .Bear:
print("It’s a Bear")
case .Elephant:
print("It’s an Elephant")
}
If you observe above example we defined enumeration “Zoo” and checking variable with switch case statement to perform required statements based on variable value.
When we run the above program in swift playground we will get a result like as shown below.
This is how we can use swift enumeration with switch-case statements to perform required statements based on the defined conditions in swift programming language.
In swift enumerations we can store associated values of any type along with the case values. The associated values will help us to store additional custom information along with the case values in swift enumerations.
In swift enumerations we can store any type of associated values but the value types can be different from each case of the enumeration based on our requirements.
For example, if we want to store marks of students along with their names in Student enumeration then by using associated values we can store easily based on our requirements.
Now we will see how to use associated values with enumeration in a swift programming language.
Following is the example of defining associated values for enumeration case statements in a swift programming language.
enum Student {
case Name(String)
case Marks(Int,Int,Int)
}
var stname = Student.Name("Suresh Dasari")
var stmarks = Student.Marks(50, 70, 85)
//It Prints Name
switch stname {
case .Name (let studentName):
print("Student Name: \(studentName)")
case .Marks(let subject1, let subject2, let subject3):
print("Student Marks: \(subject1), \(subject2), \(subject3)")
default:
print("No Record Exists")
}
//It Print Marks
switch stmarks {
case .Name (let studentName):
print("Student Name: \(studentName)")
case .Marks(let subject1, let subject2, let subject3):
print("Student Marks: \(subject1), \(subject2), \(subject3)")
default:
print("No Record Exists")
}
If you observe above example we defined associated values of student along with case statements in swift programming language.
When we run the above program in swift playground we will get a result like as shown below
Student Name: Suresh Dasari
Student Marks: 50, 70, 85
This is how we can use associated values with enumerations in a swift programming language based on our requirements.
As we discussed in swift enumerations associated values are used to store values of any type along with the case values. As an alternative to associated values, enumeration cases can come with pre-populated default values that are called raw values.
In swift enumeration, raw values can be int, string, character, etc. and the raw value will be the same type for all the members in enumeration. The raw value for each member in enumeration must be unique.
Following is the example of assigning raw values to enumeration members in swift programming languages.
enum Zoo: Int {
case Tiger = 1
case Lion = 2
case Monkey = 3
case Bear = 4
case Elephant = 5
}
var lionVal = Zoo.Lion.rawValue
print(lionVal)
var elephantVal = Zoo.Elephant.rawValue
print(elephantVal)
If you observe above example we defined enumeration “Zoo” with integer type and assigned default values of same type to each member in swift programming language.
When we run the above program in swift playground we will get a result like as shown below
2
5
If we didn’t assign any raw values to enumeration case members explicitly, swift automatically will assign values for each member. Suppose if we defined enumeration with integer values, swift will assign raw values start from 0.
Following is the example enumeration with integer values without any defined raw values in a swift programming language.
enum Zoo: Int {
case Tiger
case Lion
case Monkey
case Bear
case Elephant
}
var tigerVal = Zoo.Tiger.rawValue
print(tigerVal)
var elephantVal = Zoo.Elephant.rawValue
print(elephantVal)
When we run above example in swift playground we will get result like as shown below.
0
4
Suppose if we defined enumeration with string values, swift will assign implicit value of each case is the raw value.
Following is the example enumeration with string values without any defined raw values in a swift programming language.
enum Zoo: String {
case Tiger
case Lion
case Monkey
case Bear
case Elephant
}
var tigerVal = Zoo.Tiger.rawValue
print(tigerVal)
var elephantVal = Zoo.Elephant.rawValue
print(elephantVal)
When we run above example in swift playground we will get result like as shown below.
Tiger
Elephant
This is how we can use raw values with enumeration members in swift programming language based on our requirements.
In swift recursive enumeration is an enumeration that has another instance of the enumeration as the associated value for one or more of the enumeration cases
By using indirect keyword we can indicate that enumeration is a recursive and the compiler will insert the necessary layer of indirection.
Following is the example of using recursive enumeration in a swift programming language.
indirect enum ArithmeticExpression {
case num(Int)
case add(ArithmeticExpression, ArithmeticExpression)
}
func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case .num(let value):
return value
case .add(let first, let last):
returnevaluate(first) + evaluate(last)
}
}
let a = ArithmeticExpression.num(34)
let b = ArithmeticExpression.num(234)
let addition = ArithmeticExpression.add(a, b)
print(evaluate(addition))
If you observe above example we defined recursive enumeration and performing addition operation in swift.
When we run above example in swift playground we will get result like as shown below.
This is how we can use recursive enumeration in swift programming language based on our requirements.