Reducing typo errors with enum
Have you ever got no error during compiling and the code does not run as expected when you run it. It can be probably due to typo error in short meaning to typing errors when writing a code.
This type of errors occur usually while working with static string or static integer values. A mistake in the spelling won’t show compile time error but does create a problem during the runtime.
What can be done not to encounter the problem?
Using a set of enum solves the problem. You can create your own enum if the possible values are limited in number in order to reduce typing error.
Let us assume we are currently working on a game and we have to restrict the user’s movement to only four directions i.e. up, down, left and right. We can use enum to do so. Want to know how?
enum Movement: Int { case left = 0
case right
case up
case down}
You can use the value of enum to check whether or not to trigger some action.
let move = user.getMovement()switch move { case .left:
print(“user is moving toward left”)
case .right:
print(“user is moving toward right”)
case .up:
print(“user is moving up”)
case .down:
print(“user is moving down”)}
Here by using enum there is little or no chance of typing error due to the use of enum. Imagine if you were using static integer values to check and write the functionality to move users, I am sure you will be confused which integer value resembles which direction quite often.
Swift supports the following types for the value of an enum:
Integer
Floating point
String
Boolean
If you want to access the value associated with an enum case, you can do that from accessing the raw value property.
Don’t know about enum raw values & associated values? Want to learn about enum in depth? Please refer to my other article if you want.
Bonus Tip on benefits of using enum
Suppose that we have to make a settings page as above in our app and we will be using a static tableview for achieving this design. When the action has to be defined for each row what we normally do is check the row value of indexPath of the tableview.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {if indexPath.row == 0 {
//go to locations page
} else if indexPath.row == 1 {
//go to contacts page
} else if indexPath.row == 2 {
//go to calendar page
} else if indexPath.row == 3 {
//go to reminder page
}}
Is this way wrong? Totally not!
But imagine we have 20 rows and we have to add a row between the 10th and 11th row. Now the 11th row becomes a new one which will have a new functionality and the previous conditions after the new insertion of new value should be incremented. Sounds frustrating right? I know the pain. Lets see what can be an alternative.
Use enum to define row indexes.
enum Row: Int { case locations = 0,
contacts,
calendar,
reminder}
Use enum value to decide the functionality while clicking the row.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {if let row = Row(rawValue: indexPath.row) {
switch row {
case .locations:
print(“Go to Location page”)
case .contacts:
print(“Go to Contact page”)
case .calendar:
print(“Go to Calendar page”)
case .reminder:
print(“Go to Reminder page”)
}
}}
Now let’s imagine the same condition of adding a new row in between the rows. Simply add case in enum maintaining its position. Add the new case in didSelectRow function of tableview anywhere you like. Now run your code. Simple right? Yes, using enum saved you from disaster.