Swift
Enumeration
Swift Programming
Enum Name
iOS Development

How to get the name of enumeration value in Swift?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Enumerations, also known as enums, are a powerful feature in Swift enabling you to define a common type for a group of related values and work with those values in a type-safe manner. Sometimes, you may need to get the name of an enumeration value as a string. This can be useful for logging, debugging, or user interface display purposes. In this article, we'll explore different methods to achieve this in Swift, providing examples and technical explanations along the way.

Understanding Enumerations in Swift

Before delving into extracting the name of an enum value, let's quickly recap what enumerations are and how they are defined in Swift.

swift
1enum CompassPoint {
2    case north
3    case south
4    case east
5    case west
6}
7
8var direction = CompassPoint.north

In the example above, we defined an enum CompassPoint with four possible values. Enumerations in Swift are first-class types, allowing them to have instance methods and computed properties.

Getting the Name of an Enum Case

1. Using CustomStringConvertible

Swift's enums do not natively support retrieving a string representation of an enum case. To achieve this functionality, you can conform your enum to the CustomStringConvertible protocol, which requires you to implement the description property.

swift
1enum CompassPoint: CustomStringConvertible {
2    case north
3    case south
4    case east
5    case west
6    
7    var description: String {
8        switch self {
9        case .north: return "north"
10        case .south: return "south"
11        case .east: return "east"
12        case .west: return "west"
13        }
14    }
15}
16
17let direction = CompassPoint.north
18print("Direction: \(direction)") // Output: Direction: north

2. Using rawValue with String-type Enum

If you define your enum with raw values and set them as strings, you can directly access the rawValue to get the case name.

swift
1enum CompassPoint: String {
2    case north = "north"
3    case south = "south"
4    case east = "east"
5    case west = "west"
6}
7
8let direction = CompassPoint.north
9if let directionName = direction.rawValue {
10    print("Direction: \(directionName)") // Output: Direction: north
11}

3. Using Reflection

Swift’s reflection capabilities can be leveraged to extract an enum case name without explicitly defining strings. Here's how you can use the String(describing:) initializer:

swift
let direction = CompassPoint.north
print("Direction: \(String(describing: direction))") // Output: Direction: north

The String(describing:) function uses the custom description if provided, or otherwise defaults to using the internal representation, which for enums is typically the case identifier name.

Summary Table

The following table summarizes the key methods to retrieve the name of an enum case in Swift:

MethodDescription
CustomStringConvertibleConform the enum to CustomStringConvertible and implement the description.
Raw Value EnumDefine the enum with String raw values and access rawValue.
ReflectionUse String(describing:) for a simple and direct solution.

Additional Considerations

  • Performance: CustomStringConvertible is recommended for larger enumerations, as it provides compile-time safety and can be more descriptive.
  • Localization: If you need localized names of enum cases, using raw values may require additional handling to support localization.
  • Complex Enum Cases: For enums with associated values or complex cases, reflection and raw value methods might not be directly applicable, necessitating custom logic.

Conclusion

Obtaining the name of an enumeration value can enhance usability, logging, and debugging in Swift applications. By using the methods described above, you can effectively integrate this functionality into your projects, choosing the most appropriate technique based on your specific needs and the complexity of your enums. Whether by using protocols like CustomStringConvertible or leveraging the simplicity of String(describing:), Swift provides flexible options for working with enumeration cases.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.