Discover how to add variables and methods to enumerations in iOS development. This tutorial offers step-by-step instructions on how to use these functions in Xcode, an integrated development environment for macOS.
Key Insights
- The tutorial covers how to add variables to enums, add methods to enums and flipping values within Xcode.
- Enumerations, or enums, are a means of creating own data type in Swift. They are used to group related values together.
- Adding variables to enums helps associate a second type with the enum. The example provided associates a number with every day of the work week.
- Methods can also be added to enums, allowing the modification of properties of a structure or enumeration within a particular method.
- A mutating function can modify properties of a structure or enumeration from within the method, updating the original enum once the method ends.
- The tutorial also demonstrates how to flip values within an enum, changing the enum's associated type and the variable's returned value.
Learn how to add variables and methods to enumerations in iOS development with this comprehensive tutorial that covers topics such as setting up in Xcode, creating and using enumerations, adding variables to enums, adding methods to enums, and flipping values.
This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.
Topics Covered in This IOS Development Tutorial:
Adding Variables to Enums, Adding Methods to Enums, Flipping Values
Exercise Overview
In this exercise, we’ll see how we can add variables and methods to enumerations.
Getting Started
Launch Xcode if it isn’t already open.
Go to File > New > Playground.
Under iOS, double–click on Blank.
Navigate into Desktop > Class Files > yourname-iOS App Dev 1 Class.
Save the file as: Enums-methods.playground
Click Create.
Creating & Using Enumerations
In your new Playground file, delete all the code that’s there.
-
Create a DayOfTheWorkWeek enumeration with an associated type String as shown below:
enum DayOfTheWorkWeek: String { }
-
To get rid of the red error, add the cases to the DayOfTheWorkWeek enum shown below:
enum DayOfTheWorkWeek: String { case monday = "Monday" case tuesday = "Tuesday" case wednesday = "Wednesday" case thursday = "Thursday" case friday = "Friday"
-
Add the workDay variable shown below:
case friday = "Friday" } var workDay = DayOfTheWorkWeek.friday
-
Add the print statement:
var workDay = DayOfTheWorkWeek.friday print(workDay)
In the right sidebar, you should see friday. This is just the case value.
-
To get the string value we assigned to the case, add the bold print statement:
print(workDay) print(workDay.rawValue)
In the right sidebar, you should see Friday.
Adding Variables to Enums
-
We know we can associate one type to an enum but what if we wanted to associate a second type? For example, we’d like to associate a number with every day of the work week. To do this, we can add a variable to the enumeration. Add the bold code before the closing enum curly brace as shown below:
case friday = "Friday" var workDayNumber: Int { switch self { } } }
This switch statement is going to evaluate the value of self, the instance of the enumeration that in this case is running the code.
-
Add the switch cases for each of the enum values shown below (you may need to change the indentation for the cases to make it easier to read):
var workDayNumber: Int { switch self { case.monday: return 1 case.tuesday: return 2 case.wednesday: return 3 case.thursday: return 4 case.friday: return 5 } }
This workDayNumber variable is a computed property since it is not actually storing anything. It is drawing information from the enum and returning an integer based on each case.
-
Let’s access the workDayNumber variable. At the bottom of the playground, add the code shown below:
print(workDay) print(workDay.rawValue) workDay.workDayNumber
In the right sidebar you should see 5.
Adding Methods to Enums
-
Not only can we add variables to enums, but we can also add methods. At the bottom of the enum below the workDayNumber variable, add the advance() function shown below.
case.friday: return 5 } } mutating func advance() { }
A mutating function can be used to modify the properties of a structure or enumeration within a particular method. The method will mutate its properties from within the method, and the changes will be updated in the original enum once the method ends.
-
We’ll use the mutating method to assign a completely new instance to the implicit self property, and this new instance will replace the existing one once the method ends. Add the switch statement with the cases shown below:
mutating func advance() { switch self { case.monday: self =.tuesday case.tuesday: self =.wednesday case.wednesday: self =.thursday case.thursday: self =.friday case.friday: self =.monday } }
Remember that in our example, self is always workDay—the instance of the enumeration which is running the code. Each of the cases are changing (or advancing) the workDay to the next, so for example.monday becomes.tuesday and so forth.
-
Let’s test the advance() function. At the bottom of the playground add the following:
workDay.workDayNumber workDay.advance()
In the right sidebar, you should see monday. The advance() function has changed friday to monday.
-
Get the rawValue by adding the following in bold:
workDay.advance() workDay.rawValue
You should see Monday.
-
Let’s check the new workDayNumber. Add the bold code below:
workDay.advance() workDay.workDayNumber
In the right sidebar, you should see 1 so we know it’s working.
Flipping Values
Let’s see one last example. This time, we’ll give the enum an Int associated type, and the variable will return the workDay name instead. Essentially we’ll be flipping the enum and variable values.
We can use the code we already have. Select all the code in the playground and copy it.
Paste the code directly below. Ignore all the red errors, we’ll fix them shortly.
-
We’re going to change the enum to an Int type. Change the bold code below:
enum DayOfTheWorkWeekInt: Int { case monday = 1 case tuesday = 2 case wednesday = 3 case thursday = 4 case friday = 5 }
-
Now change the variable to a String type and update the values as shown in bold:
var workDayName: String { switch self { case.monday: return "Monday" case.tuesday: return "Tuesday" case.wednesday: return "Wednesday" case.thursday: return "Thursday" case.friday: return "Friday" } }
Remember that this variable is a computed property and nothing is actually stored inside it. The block of code inside the curly braces simply evaluates each case and returns the string attached. Take note that this property also has an implicit getter (it’s automatically assigned) and no setter. It can only set other properties and values indirectly. We’ll see this more clearly shortly.
NOTE: When a property only has a getter, and no setter we do not need to add the keyword get. This means our code is equivalent to the code shown below.
get { switch self { case.monday: return "Monday" case.tuesday: return "Tuesday" case.wednesday: return "Wednesday" case.thursday: return "Thursday" case.friday: return "Friday" } }
-
Let’s fix the red error by assigning a new variable name. Edit the code as shown in bold below:
var workDay2 = DayOfTheWorkWeekInt.friday print(workDay2) print(workDay2.rawValue) workDay2.workDayName workDay2.advance() workDay2.workDayName
In the right sidebar, take a look at the new values. Next to workDay2.rawValue you should see the newly assigned 5 Int type, and the workDayName should now be Friday. The advance() method should change the values to 1 and Monday.
-
Let’s see if we can set the workDay2.workDayName to a new variable. Add the code shown below:
workDay2.workDayName workDay2.workDayName = "Tuesday"
-
Next to this line, you should see a red error
. Click it to see that you cannot assign to property workDayName as it’s a get-only property.
We just wanted to show you that the computed property workDayName has an implicit getter so you can delete this line.
Save and close the file. Keep Xcode open, as we’ll use it in the next exercise.