In this tutorial you will learn about the Swift Enumerations and its application with practical example.
Swift Enumerations
An Enumeration is a first-class data type in swift.It is consist of a set of predefined named values, called as members. Enumerations are useful when we want deal with limited set of values for variable. For example you can think of the colors of a traffic light can only be one of three colors– red, yellow or green.
Swift enumerations are much powerful yet flexible, we can access its values in a type-safe manner within your code. Enumeration values can be a string, a character, or a value of any integer or floating-point type.
Defining an enumeration
In Swift, enumeration can be declared using the enum keyword followed by a list of the individual members introduced between curly brackets {} using the case keyword.
Syntax:-
1 2 3 4 5 6 7 |
enum enumname{ //Case A //Case B //Case C --------- //Case N } |
Example:-
Let’s define an enumeration for days of week –
1 2 3 4 5 6 7 8 9 |
enum DaysofWeek { case Sun case Mon case Tue case Wed case Thu case Fri case Sat } |
Dot syntax
Dot syntax can be used to refer enumeration’s member if the type of an enumeration is known or can be inferred.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
enum DaysofWeek { case Sun case Mon case Tue case Wed case Thu case Fri case Sat } print("W3Adda - Enumeration dot syntax.") // in this case the type is known var toDay: DaysofWeek = .Mon // in this case the type can be inferred if toDay == .Mon { print("It's Monday.") } |
Output:-
Swift Enumeration with Switch Statement
In Swift, an enumeration can be used in switch statement as following –
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
enum DaysofWeek { case Sun case Mon case Tue case Wed case Thu case Fri case Sat } print("W3Adda - Switch Statement using Enumeration.") var toDay: DaysofWeek = .Mon switch toDay { case .Sun: print("It's Sunday.") case .Mon: print("It's Monday.") case .Tue: print("It's Tuesday.") case .Wed: print("It's Wednesday.") case .Thu: print("It's Thursday.") case .Fri: print("It's Friday.") case .Sat: print("It's Saturday.") } |
Output:-
Swift Enumeration with Raw Values
In Swift Enumeration members can be associated with a prepopulated raw value of type Int, String, Character, etc. All of the enum member should have raw values of same type and must be unique.If integers are used for raw values, they auto-increment if no value is specified for any members.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 |
enum DaysofWeek: String { case Sun = "Sunday" case Mon = "Monday" case Tue = "Tuesday" case Wed = "Wednesday" case Thu = "Thursday" case Fri = "Friday" case Sat = "Saturday" } print("W3Adda - Enumeration with Raw Value.") print(DaysofWeek.Sat.rawValue) print(DaysofWeek.Sun.rawValue) |
Output:-
Swift Enumeration with Associated Values
In associated values we can store data with member of an enumeration, which changes between different instances of an enumeration. With associated values you can associate a tuple with multiple to a member of enumerations, this tuple can be of different type for each of the enumeration members.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
enum studentInfo { case stdName(String) case stdMarks(Int,Int,Int,Int) } var sname = studentInfo.stdName("Keith") var smarks = studentInfo.stdMarks(75,80,90,95) print("W3Adda - Enumeration with Associated Value.") switch sname { case .stdName(let sname): print("Student name is: \(sname).") case .stdMarks(let Mark1, let Mark2, let Mark3, let Mark4): print("Student Marks are: \(Mark1),\(Mark2),\(Mark3),\(Mark4)") } switch smarks { case .stdName(let sname): print("Student name is: \(sname).") case .stdMarks(let Mark1, let Mark2, let Mark3, let Mark4): print("Student Marks are: \(Mark1),\(Mark2),\(Mark3),\(Mark4)") } |
Output:-
Iterating over Enumeration Cases
In swift, it is possible to iterate over the enumeration cases, for this we have to enable this property by adding CaseIterable after the enumeration name, then swift exposes a collection of all the cases in an allCases property of the enumeration type.Once all set, we can loop through the enum members using for-in loop as following –
Example:-
1 2 3 4 5 6 7 |
enum DaysofWeek: CaseIterable { case John, Steve, Keith, Alex } for day in DaysofWeek.allCases { print(day) } |
Output:-
1 2 3 4 |
John Steve Keith Alex |
Note:- Above Syntax must comply with the CaseIterable protocol.
Recursive Enumerations
An enumeration with another instance of the enumeration as the associated value for one or more of the enumeration is termed as recursive enumeration.A indirect keyword is used to indicate that enumeration is a recursive.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
indirect enum ArithmeticExpression { case num(Int) case mul(ArithmeticExpression, ArithmeticExpression) } func evaluate(_ expression: ArithmeticExpression) -> Int { switch expression { case .num(let value): return value case .mul(let n1, let n2): return evaluate(n1) * evaluate(n2) } } let a = ArithmeticExpression.num(7) let b = ArithmeticExpression.num(5) let result = ArithmeticExpression.mul(a, b) print("W3Adda - Recursive Enumeration") print(evaluate(result)) |
Output:-