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.
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.
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.
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.
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.
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.
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
rawValuewhen the enum needs a stable serialized string. - Use a computed property for display text.
- Use
CustomStringConvertiblewhen 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
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.