An enumeration type appears in every programming language, Enum stores the finite set of data like string, integer, enum is a list of comprehensive raw data in swift, enum is useful for handling flag, states, choices with respect of object and functionality. enum type plays a very important role in development to handle the lifecycle of operations and many more.
let us see, how enum works in swift, enum is declared by enum keyword, with enum type variable name, by using case keyword you can define values in the enum, here is how enum is declared in swift.
1 2 3 4 5 6 |
enum StatusType { case OPEN case SUSPENDED case COMPLETED case CANCELED } |
StatusType enum doesn’t represent any ideal characteristics of any of the individual case, it just represents the Status type, we can return raw value in term of the particular type case, here is how we can represent value for listed cases.
1 2 3 4 5 6 |
enum StatusType: Int32 { case OPEN = 1 case SUSPENDED = 2 case COMPLETED = 3 case CANCELED = 4 } |
you can return value of enum type case.
1 2 |
let status = StatusType.SUSPENDED.rawValue print(Status) // 2 |
As we discussed, we can handle functionality as per status or choice of an object, We can handle enum type in switch case conditions also, to perform an operation with respect to the particular case.
1 2 3 4 5 6 7 8 9 10 11 12 |
switch Status { case .StatusType.CANCELED.rawValue: // Cancel status code case .StatusType.COMPLETED.rawValue: // Completed status code case .StatusType.SUSPENDED.rawValue: // suspended status code case .StatusType.OPEN.rawValue: // open status code default: // Default implementation } |
Descriptive enum:
We have applied raw value to enum which is Int32 data type, we can send this value to web service or store in core data, what if we want to have the descriptive value of the enum cases, we can write a function inside enum to get the descriptive value of respective enum case, here I have an example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
enum StatusType: Int32 { case OPEN = 1 case SUSPENDED = 2 case COMPLETED = 3 case CANCELED = 4 static func getValueBy(statusTypeId: Int32) -> String { switch statusTypeId { case 1: return "Open" case 2: return "Suspended" case 3: return "Completed" case 4: return "Cancelled" default: return "" } } } |
Now i will try to fetch descriptive value of an particular enum case.
1 2 3 |
let openStatus = StatusType.OPEN.rawValue let descriptiveValue = StatusType.getValueBy(statusTypeId: openStatus) print(descriptiveValue) // "Open" string will print |
Identical type Enum cases:
Do not worry, I will explain to you briefly how to implement identical test cases. enum doesn’t associate raw value, we can have a different identical value of each case. let us start with an example, we will track Task Status by identical enum type cases.
First we will create one model for Task.
1 2 3 4 |
struct Task { var TaskName: String var TaskId: String } |
Create type enum with identical values. here we are giving different input values for different cases like.
1) OPEN : open will not have input value.
2) STARTED: The started case will have Date() input value, which will be the task Started Date.
3) SUSPENDED: Suspended Case will have string input values, which will be the reason for suspension.
4) COMPLETED: Completed Case will have Date() input values, which will be the date of task completion.
5) CANCELED: Canceled Case will have string input values, which will be the reason for cancelation.
1 2 3 4 5 6 7 |
enum StatusType { case OPEN case STARTED(startDate: Date) case SUSPENDED(reason: String) case COMPLETED(endDate: Date) case CANCELED(reason: String) } |
Create model for handle task status. Default StatusType declared as .OPEN
1 2 3 4 |
struct TaskDetail { var task: Task var StatusType: StatusType = .OPEN } |
Implemented update StatusType function below, which will perform the operation of regarded enum case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
func update(status: StatusType) { switch status { case .OPEN: print("Task is still open") break case .STARTED(let startDate): print("Task started date \(startDate)") break case .SUSPENDED(let suspentionReason): print("Task suspention reason \(suspentionReason)") break case .COMPLETED(let completedDate): print("Task completed date \(completedDate)") break case .CANCELED(let cancelationReason): print("Task cancelation reason \(cancelationReason)") break } } |
Here is how identical enum updating task status model
1 2 3 4 5 |
update(status: .OPEN) // Task is still open update(status: .STARTED(startDate: Date())) // Task started date 2020-04-26 17:39:04 +0000 update(status: .SUSPENDED(reason: "under qualified")) // Task suspention reason under qualified update(status: .CANCELED(reason: "material is not available")) // Task cancelation reason material is not available update(status: .COMPLETED(endDate: Date())) // Task completed date 2020-04-26 17:40:59 +0000 |
CaseIterable:
CaseIterable is a protocol returns all values of collection. CaseIterable protocol can be conformed to the enum.
1 2 3 4 5 6 7 8 9 |
enum StatusType: Int32, CaseIterable { case OPEN = 1 case SUSPENDED = 2 case COMPLETED = 3 case CANCELED = 4 } let caseList : [StatusType] = StatusType.allCases.map{$0} print(caseList) // return all StatusType enum cases |
Conclusion:
Enum is an extreme and adaptable type in swift, enum plays a significant role in data binding while developing Swift Application. Handle states, flags, and thought code architecture, There are many more protocols that can conform with enum type like equatable.