Enumerations, commonly known as enums, are Swift’s powerful and flexible feature. They allow you to define a common type for a group of related values and work with those values in a type-safe way within your code. Here’s a closer look at what makes enums so valuable in Swift development:

Enums are a crucial topic in iOS app development. Today, we will discuss the following things:

· What are Enumerations?
· Importance of Enums in iOS Development
· Associated Values
· Raw Values
· Recursive Enumerations
· Enum Methods and Properties
· Using Enums with Switch Statements
· Using Enums in a To-Do List App
· Enums Best Practices
· Common Pitfalls of Using Enums
· Resources
· Conclusion

What are Enumerations?

Enumerations, or enums, are a way to group related values under a single type. Each value defined in an enumeration is known as a case. Unlike enums in other programming languages, Swift enums are highly versatile and can store associated and raw values, making them more powerful and expressive.

Importance of Enums in iOS Development

Enums are crucial in iOS development for several reasons:

Type Safety: Enums help ensure that your code works with valid values, reducing the likelihood of bugs.Readability and Maintainability: By grouping related values, enums make your code more readable and easier to maintain.Pattern Matching: Enums work seamlessly with Swift’s pattern-matching capabilities, such as switch statements, allowing for more concise and readable code.Flexibility: Swift enums can have associated values, enabling them to store additional information, and raw values, which allow them to conform to a certain type like Stringor Int.

Here’s a simple example to illustrate the basic syntax of an enum in Swift:

enum Direction {
case north
case south
case east
case west
}

You can use an enum case like this:

let currentDirection = Direction.north

And with a switch statement:

switch currentDirection {
case .north:
print(“Heading north”)
case .south:
print(“Heading south”)
case .east:
print(“Heading east”)
case .west:
print(“Heading west”)
}

Enums are useful for defining a finite set of options or states, which can be particularly helpful in managing states in an app, handling errors, or representing constant values.

Associated Values

Enums in Swift can store associated values of any type, allowing each case to have additional information. This feature is particularly useful for representing more complex data structures.

Here’s an example with associated values:

enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}

var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode(“ABCDEFGHIJKLMNOPQRSTUVWXYZ”)

You can extract the associated values using a switch statement:

switch productBarcode {
case .upc(let systemId, let manufacturerId, let productId, let batchNumber):
print(“UPC: (systemId), (manufacturerId), (productId), (batchNumber).”)
case .qrCode(let productCode):
print(“QR Code: (productCode)”)
}

Raw Values

Enums can also store raw values, which are default values for each case. These raw values must be of the same type and are defined when the enum is created. Raw values are typically used for cases where the values are already known, like in ASCII codes or HTTP status codes.

Here’s an example with raw values:

enum ASCIIControlCharacter: Character {
case tab = “t”
case lineFeed = “n”
case carriageReturn = “r”
}

let newLine = ASCIIControlCharacters.lineFeed.rawValue
print(“New Line character: (newLine)”)

You can also initialize an enum case from a raw value:

let possibleCharacter = ASCIIControlCharacter(rawValue: “n”)

Enums in Swift provide a robust way to manage related values, enhance code readability, and maintain type safety. Whether you’re using associated values for more complex data or raw values for known constants, enums are a versatile tool in your Swift programming toolkit.

Recursive Enumerations

Recursive enumerations are enums that have cases that are associated with the enum itself. This is useful for representing data structures that are naturally recursive, such as linked lists or trees.

To define a recursive enum, use the indirect keyword. Here’s an example of a recursive enum representing a simple arithmetic expression:

enum ArithmeticExpression {
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}

You can also mark the entire enum as indirect if all cases are recursive:

indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}

Evaluating an arithmetic expression using a recursive enum can be done with a switch statement:

func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case .number(let value):
return value
case .addition(let left, let right):
return evaluate(left) + evaluate(right)
case .multiplication(let left, let right):
return evaluate(left) * evaluate(right)
}
}

let expression = ArithmeticExpression.addition(.number(5), .multiplication(.number(2), .number(3)))
print(“Result: (evaluate(expression))”) // Output: Result: 11

Enum Methods and Properties

Enums in Swift can have instance methods and computed properties, which allows you to add functionality directly to the enum cases.

Here’s an example of an enum with methods and properties:

enum CompassPoint {
case north
case south
case east
case west

var description: String {
switch self {
case .north:
return “North”
case .south:
return “South”
case .east:
return “East”
case .west:
return “West”
}
}

func distance(to point: CompassPoint) -> String {
// Example logic for distance calculation
if self == point {
return “You are already at (point.description)”
} else {
return “Moving from (self.description) to (point.description)”
}
}
}

let direction = CompassPoint.north
print(direction.description) // Output: North
print(direction.distance(to: .south)) // Output: Moving from North to South

Using Enums with Switch Statements

Switch statements are a powerful way to work with enums. They allow you to handle each case explicitly, ensuring that all possible cases are covered. This can lead to more robust and maintainable code.

Here’s an example with a more complex enum and a switch statement:

enum Beverage {
case coffee(size: String)
case tea(type: String, sweetness: String)
case juice(flavor: String)
}

let order = Beverage.tea(type: “Green”, sweetness: “Medium”)

switch order {
case .coffee(let size):
print(“Ordered a (size) coffee.”)
case .tea(let type, let sweetness):
print(“Ordered a (type) tea with (sweetness) sweetness.”)
case .juice(let flavor):
print(“Ordered a (flavor) juice.”)
}

Using enums with switch statements ensures that you handle each case appropriately, making your code more predictable and easier to debug.

Using Enums in a To-Do List App

Let’s consider a To-Do List app where tasks can have different priorities and states. Enums can be used to represent these priorities and states.

Defining Task Priority and State

enum TaskPriority: Int {
case low = 1
case medium = 2
case high = 3
}

enum TaskState {
case pending
case inProgress
case completed
}

Task Model

struct Task {
var title: String
var priority: TaskPriority
var state: TaskState
}

You can use enums to filter tasks by priority or state, making the app logic clean and readable.

var tasks = [
Task(title: “Buy groceries”, priority: .medium, state: .pending),
Task(title: “Finish project report”, priority: .high, state: .inProgress),
Task(title: “Book flight tickets”, priority: .low, state: .completed)
]

func filterTasks(by priority: TaskPriority) -> [Task] {
return tasks.filter { $0.priority == priority }
}

let highPriorityTasks = filterTasks(by: .high)
print(highPriorityTasks.map { $0.title }) // Output: [“Finish project report”]

Enums make it easy to manage and filter tasks, ensuring type safety and clarity in your code.

Enums for API Handling

Enums can represent different types of API responses, allowing for clear and concise handling of each response type.

enum APIResponse {
case success(data: Data)
case failure(error: Error)
}

func handleResponse(_ response: APIResponse) {
switch response {
case .success(let data):
// Process the data
print(“Data received: (data)”)
case .failure(let error):
// Handle the error
print(“Error occurred: (error)”)
}
}

State Management

Enums can represent different states of a view or a network request, making it easier to manage state transitions.

enum ViewState {
case loading
case loaded(data: [String])
case error(message: String)
}

func updateView(for state: ViewState) {
switch state {
case .loading:
// Show loading indicator
print(“Loading…”)
case .loaded(let data):
// Display the data
print(“Data loaded: (data)”)
case .error(let message):
// Show error message
print(“Error: (message)”)
}
}

var currentState = ViewState.loading
updateView(for: currentState)
currentState = .loaded(data: [“Item 1”, “Item 2”])
updateView(for: currentState)

Enums help maintain clear and manageable state transitions, improving the reliability and readability of your code.

Enums Best Practices

Use Descriptive Case Names: Ensure your enum case names are descriptive and convey the purpose.enum UserRole {
case admin
case user
case guest
}Leverage Associated Values: Use associated values to add relevant data to enum cases, providing more context and reducing the need for additional properties.enum Media {
case photo(fileName: String)
case video(url: URL, duration: Int)
}Switch Statements: Use switch statements to handle all possible cases, ensuring your code is exhaustive and less error-prone.func handleRole(_ role: UserRole) {
switch role {
case .admin:
print(“Admin access”)
case .user:
print(“User access”)
case .guest:
print(“Guest access”)
}
}Use Raw Values When Appropriate: Use raw values when you need to map enum cases to a specific type, such as strings or integers.enum HTTPStatusCode: Int {
case ok = 200
case notFound = 404
}

Common Pitfalls of Using Enums

Overusing Enums: Avoid using enums for types that are likely to change frequently, as adding new cases requires updating all switch statements handling that enum.Complex Nested Enums: Be cautious with deeply nested enums, as they can make your code harder to read and maintain.Default Cases in Switch Statements: Avoid using default cases in switch statements for enums with associated values, as it can mask new cases that are added later.switch status {
case .success(let data):
print(“Success with data: (data)”)
case .failure(let error):
print(“Failure with error: (error)”)
// Avoid using default to ensure new cases are handled explicitly
}

Resources

Enumerations: Model custom types that define a list of possible valuesallCases: A collection of all values of this typeCaseIterable: A type that provides a collection of all of its values

Conclusion

Enums are a powerful feature in Swift that can significantly enhance the structure, readability, and safety of your code. By applying these advanced techniques and best practices, you can harness the full potential of enums in your iOS projects, leading to more maintainable and robust applications.

Harnessing the Potential of Swift Enums to Build Better iOS Apps was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.

​ Level Up Coding – Medium

about Infinite Loop Digital

We support businesses by identifying requirements and helping clients integrate AI seamlessly into their operations.

Gartner
Gartner Digital Workplace Summit Generative Al

GenAI sessions:

  • 4 Use Cases for Generative AI and ChatGPT in the Digital Workplace
  • How the Power of Generative AI Will Transform Knowledge Management
  • The Perils and Promises of Microsoft 365 Copilot
  • How to Be the Generative AI Champion Your CIO and Organization Need
  • How to Shift Organizational Culture Today to Embrace Generative AI Tomorrow
  • Mitigate the Risks of Generative AI by Enhancing Your Information Governance
  • Cultivate Essential Skills for Collaborating With Artificial Intelligence
  • Ask the Expert: Microsoft 365 Copilot
  • Generative AI Across Digital Workplace Markets
10 – 11 June 2024

London, U.K.