Skip to main content
April 1, 2026Noble Desktop Publishing Team/7 min read

Enumerations, Properties & Methods

Master iOS Enumerations with Properties and Methods

Key Concepts You'll Master

Enum Variables

Learn to add computed properties to enumerations for enhanced functionality. Properties can derive values from enum cases dynamically.

Enum Methods

Discover how to add mutating functions that can modify enum instances. Methods enable sophisticated state management within enums.

Type Association

Understand how to flip between different associated types and computed properties for flexible enum design patterns.

Topics Covered in This iOS Development Tutorial:

Adding Variables to Enums, Adding Methods to Enums, Flipping Values

Exercise Overview

In this comprehensive exercise, you'll master advanced enumeration techniques in Swift by learning how to extend enums beyond simple case definitions. We'll explore adding computed properties and methods to enumerations—powerful features that transform enums from basic value containers into sophisticated data structures with behavior and intelligence.

Setup Requirements

1

Launch Xcode

Open Xcode and create a new Playground file under iOS Blank template

2

File Organization

Navigate to Desktop > Class Files > yourname-iOS App Dev 1 Class directory

3

Save Project

Save the file as Enums-methods.playground and click Create to begin

Getting Started

  1. Launch Xcode if it isn't already open. Ensure you're running the latest version compatible with your development setup.

  2. Go to File > New > Playground.

  3. Under iOS, double–click on Blank.

  4. Navigate into Desktop > Class Files > yourname-iOS App Dev 1 Class.

  5. Save the file as: Enums-methods.playground

  6. Click Create.

Creating & Using Enumerations

Let's begin by establishing a solid foundation with a basic enumeration that we'll enhance throughout this tutorial.

  1. In your new Playground file, delete all the default code that's there.

  2. Create a DayOfTheWorkWeek enumeration with an associated String type as shown below. This enum will serve as our primary example for demonstrating advanced enum capabilities:

    enum DayOfTheWorkWeek: String {
    
    }
  3. To resolve the compilation error, add the cases to the DayOfTheWorkWeek enum shown below. Notice how we're explicitly assigning string values, which gives us control over the raw value representation:

    enum DayOfTheWorkWeek: String {
    
       case monday = "Monday"
       case tuesday = "Tuesday"
       case wednesday = "Wednesday"
       case thursday = "Thursday"
       case friday = "Friday"
  4. Create an instance of the enumeration by adding the workDay variable shown below:

    case friday = "Friday"
    }
    
    var workDay = DayOfTheWorkWeek.friday
  5. Add the print statement to observe the enum's default output behavior:

    var workDay = DayOfTheWorkWeek.friday
    
    print(workDay)

    In the right sidebar, you should see friday. This displays the case name, not the associated string value.

  6. To access the string value we explicitly assigned to each case, add the bold print statement:

    print(workDay)
    print(workDay.rawValue)

    In the right sidebar, you should see Friday. The rawValue property provides access to the underlying String value we assigned to each case.

Raw Values vs Case Values

When you print an enum directly, you see the case name. Use .rawValue to access the associated String or Int value assigned to each case.

DayOfTheWorkWeek Enum Structure

Monday
1
Tuesday
2
Wednesday
3
Thursday
4
Friday
5

Adding Variables to Enums

Now we'll explore one of Swift's most powerful enum features: the ability to add computed properties that derive additional information from enum cases.

  1. While Swift allows us to associate one raw value type to an enum, we often need additional associated data. For instance, imagine we want to associate a numeric day identifier with each workday. We can achieve this elegantly using computed properties. Add the bold code before the closing enum curly brace as shown below:

    case friday = "Friday"
    
       var workDayNumber: Int {
          switch self {
    
          }
       }
    }

    This switch statement evaluates self—the current instance of the enumeration—and returns appropriate values based on the active case.

  2. Add the switch cases for each enum value shown below. Notice how we can return different integer values for each case, effectively creating a second "associated value" system:

    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 is a computed property—it doesn't store data but instead calculates and returns values dynamically. This approach is memory-efficient and ensures the derived data always stays consistent with the enum case.

  3. Let's test the workDayNumber computed property. 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, confirming that Friday correctly maps to day 5 of the work week.

Computed Properties in Enums

The workDayNumber variable is a computed property that doesn't store data but derives values from enum cases using switch statements.

Enum Cases vs Computed Properties

FeatureRaw ValueComputed Property
StorageStores actual dataCalculates dynamically
Access Method.rawValue.workDayNumber
Memory UsageFixed allocationNo storage overhead
FlexibilityStatic assignmentDynamic calculation
Recommended: Use computed properties when you need derived values that change based on enum state

Adding Methods to Enums

Swift enums can contain methods just like classes and structures, enabling sophisticated behavior and state manipulation. Let's implement a method that advances through the work week.

  1. Beyond computed properties, enums can contain methods that modify their state or perform calculations. Below the workDayNumber variable, add the advance() function shown below:

    case.friday: return 5
       }
    }
    
    mutating func advance() {
    
    }

    The mutating keyword is crucial here—it signals that this method will modify the enum instance's value. In Swift's value-type system, methods that change an instance must be explicitly marked as mutating, providing clear documentation of side effects.

  2. We'll implement the advance logic by assigning a new instance to the implicit self property. This approach replaces the current enum value with the next logical value. 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
       }
    }

    Notice how self represents the current enum instance (workDay in our example). Each case transitions to the next logical workday, with Friday cycling back to Monday—perfect for modeling a continuous work week cycle.

  3. Let's test the advance() function in action. At the bottom of the playground add the following:

    workDay.workDayNumber
    workDay.advance()

    In the right sidebar, you should see monday. The advance() method successfully transformed our Friday instance into Monday, demonstrating the enum's state mutation capability.

  4. Verify the string representation by adding the following code:

    workDay.advance()
    workDay.rawValue

    You should see Monday, confirming that all enum properties update consistently when the case changes.

  5. Let's verify the computed property also reflects the change. Add the bold code below:

    workDay.advance()
    workDay.workDayNumber

    In the right sidebar, you should see 1, proving that our computed property automatically recalculates based on the new enum state.

Mutating Functions Required

When enum methods need to modify the instance itself, you must use the mutating keyword. This allows the method to assign a new value to self.

Advance Function Logic

1

Switch on Self

The advance function uses a switch statement to evaluate the current enum value

2

Assign New Value

Each case assigns the next weekday to self, creating a cycling pattern

3

Handle Wrap-around

Friday advances back to Monday, completing the work week cycle

Flipping Values

To reinforce these concepts and demonstrate enum flexibility, let's create an alternative implementation where we flip the roles of raw values and computed properties.

  1. We can repurpose our existing code to demonstrate alternative enum design patterns. Select all the code in the playground and copy it.

  2. Paste the code directly below. Don't worry about the compilation errors—we'll systematically resolve them by adapting the enum structure.

  3. Let's redesign the enum to use Int as the raw value type instead of String. Change the bold code below:

    enum DayOfTheWorkWeekInt: Int {
    
       case monday = 1
       case tuesday = 2
       case wednesday = 3 
       case thursday = 4
       case friday = 5
    
    }
  4. Now we'll create a computed property that returns the string names. Update the variable to return String 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"
       }
    }

    This computed property demonstrates the same principles as before, but now returns string names while the raw values are integers. Notice that this property has an implicit getter—when a computed property only retrieves values without setting them, Swift automatically handles the getter mechanism.

    NOTE: Since this property only provides read access, we don't need the explicit get keyword. Our code is functionally equivalent to this more verbose version:

    get {
       switch self {
          case.monday: return "Monday"
          case.tuesday: return "Tuesday"
          case.wednesday: return "Wednesday"
          case.thursday: return "Thursday"
          case.friday: return "Friday"
       }
    }
  5. Let's resolve the naming conflicts by creating distinct variable names. Edit the code as shown in bold below:

    var workDay2 = DayOfTheWorkWeekInt.friday
    
    print(workDay2)
    print(workDay2.rawValue)
    workDay2.workDayName
    workDay2.advance()
    workDay2.workDayName

    Examine the output in the right sidebar. Next to workDay2.rawValue you should see 5 (the Int raw value), and workDayName should display "Friday". After calling advance(), these should change to 1 and "Monday", demonstrating consistent behavior across different enum designs.

  6. Let's explore the read-only nature of computed properties by attempting to modify the workDayName. Add the code shown below:

    workDay2.workDayName
    
    workDay2.workDayName = "Tuesday"
  7. Next to this line, you should see a red error red alert icon. Click it to see the message: "Cannot assign to property 'workDayName' as it's a get-only property."

    This error demonstrates an important Swift principle: computed properties with only getters are read-only by design. This prevents inconsistent state and ensures data integrity. You can delete this line since it was purely instructional.

  8. Save and close the file. Keep Xcode open for the next exercise where we'll build upon these enumeration concepts.

Int vs String Associated Types

Pros
Int types enable mathematical operations on enum values
String types provide human-readable representations
Computed properties can bridge between different data types
Flexible design allows switching primary and derived values
Cons
Get-only computed properties cannot be directly assigned
Additional complexity when managing multiple data representations
Potential confusion between raw values and computed properties
Implicit Getters in Swift

When a computed property only has a getter and no setter, Swift automatically provides implicit getter behavior. You don't need to explicitly write the get keyword.

Key Takeaways

1Enumerations in Swift can contain computed properties that derive values dynamically from enum cases using switch statements
2Mutating functions are required when enum methods need to modify the instance itself by assigning new values to self
3Raw values provide static data associated with enum cases, while computed properties offer dynamic calculations
4The advance function demonstrates how enums can implement state transitions with cycling behavior across cases
5Computed properties with only getters are read-only and cannot be directly assigned new values
6You can flip the relationship between associated types and computed properties for flexible enum design patterns
7Swift's implicit getter behavior eliminates the need for explicit get keywords in read-only computed properties
8Enum methods enable sophisticated state management and data manipulation within enumeration structures

RELATED ARTICLES