Swift
enum
convert
string
programming

Swift Convert enum value to String?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

Converting a Swift enum to a string is simple once you decide what the string is for. If the value is part of a stable external format, use a raw string value. If it is for display, use a computed property or CustomStringConvertible so UI text stays separate from the enum case name.

Use Raw String Values for Stable Serialized Output

If the string representation should be fixed and predictable, define the enum with a String raw value.

swift
1import Foundation
2
3enum Direction: String {
4    case north = "north"
5    case south = "south"
6    case east = "east"
7    case west = "west"
8}
9
10let value = Direction.north.rawValue
11print(value)

This is the best choice for API payloads, persistence keys, and configuration values because the mapping is explicit.

Use a Computed Property for User-Facing Text

UI labels often need capitalization, spacing, or wording that should not leak into the raw representation.

swift
1import Foundation
2
3enum Direction {
4    case north
5    case south
6    case east
7    case west
8
9    var displayName: String {
10        switch self {
11        case .north: return "North"
12        case .south: return "South"
13        case .east: return "East"
14        case .west: return "West"
15        }
16    }
17}
18
19print(Direction.south.displayName)

This keeps presentation concerns local and avoids turning enum case spelling into a UI contract.

Adopt CustomStringConvertible When Printing Is the Main Use Case

If the enum is mostly logged or interpolated into strings, conforming to CustomStringConvertible can make usage more natural.

swift
1import Foundation
2
3enum JobState: CustomStringConvertible {
4    case queued
5    case running
6    case finished
7
8    var description: String {
9        switch self {
10        case .queued: return "Queued"
11        case .running: return "Running"
12        case .finished: return "Finished"
13        }
14    }
15}
16
17let state: JobState = .running
18print(state)

Use this when the printed form is the intended meaning, not when you need separate machine and human representations.

Handle Enums with Associated Values Explicitly

Enums with associated values need a custom conversion because there is no single built-in string form you should trust for program logic.

swift
1import Foundation
2
3enum NetworkResult {
4    case success(code: Int)
5    case failure(message: String)
6
7    var text: String {
8        switch self {
9        case .success(let code):
10            return "Success \(code)"
11        case .failure(let message):
12            return "Failure: \(message)"
13        }
14    }
15}
16
17print(NetworkResult.success(code: 200).text)

This is better than using reflection output because you control the exact representation.

Convert All Cases to Strings for Menus and Pickers

When the enum is CaseIterable, you can generate a list of labels from all cases.

swift
1import Foundation
2
3enum Priority: String, CaseIterable {
4    case low
5    case medium
6    case high
7}
8
9let labels = Priority.allCases.map(\.rawValue)
10print(labels)

That is useful for building settings screens or form options while keeping the enum as the source of truth.

Avoid Reflection for Stable Output

Using String(describing:) can be convenient for debugging, but it should not be your long-term serialization format. The output may not match what the UI or an API requires, and it is easy to couple behavior to something that should remain an implementation detail.

Common Pitfalls

  • Using String(describing:) as a persistence or API format instead of defining an explicit mapping.
  • Reusing raw values for user-visible text that needs localization or better wording.
  • Mixing machine-readable and display-readable strings in the same enum property.
  • Assuming associated-value enums have a meaningful default string representation.
  • Spreading switch-based string mappings across the codebase instead of centralizing them on the enum.

Summary

  • Use rawValue when the enum needs a stable serialized string.
  • Use a computed property for display text.
  • Use CustomStringConvertible when printing is the primary use case.
  • Add explicit conversion logic for associated-value enums.
  • Keep external formats and UI labels separate from enum case spelling.

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.