Loading...

How do get the string value of an enum in Swift?

question swift
Ram Patra Published on November 17, 2023

In Swift, to get the string value of an enum, you typically have a couple of approaches depending on the enum’s definition. Let’s go through them:

  1. Enum with String Raw Values: If your enum is defined with String as its raw type, you can directly get the string value using the rawValue property.

    enum MyEnum: String {
        case firstCase = "First"
        case secondCase = "Second"
    }
    
    let enumValue = MyEnum.firstCase
    let stringValue = enumValue.rawValue  // "First"
    

    In this example, MyEnum is an enum with String raw values. Each case is assigned a specific String value.

  2. Enum Without Specific String Values: If your enum doesn’t have String as its raw type (or doesn’t have a raw type at all), you can still convert its case names into strings using reflection with the String(describing:) initializer or the "\()" syntax.

    enum MyEnum {
        case firstCase
        case secondCase
    }
    
    let enumValue = MyEnum.firstCase
    let stringValue = String(describing: enumValue)  // "firstCase"
    // or
    let stringValue2 = "\(enumValue)"  // "firstCase"
    

    Here, the name of the case (firstCase, secondCase, etc.) is converted into a string.

  3. Custom Method for String Conversion: If you need more control or customization over the string values, you might implement a computed property or method in your enum.

    enum MyEnum {
        case firstCase
        case secondCase
    
        var stringValue: String {
            switch self {
            case .firstCase:
                return "Custom String for First Case"
            case .secondCase:
                return "Custom String for Second Case"
            }
        }
    }
    
    let enumValue = MyEnum.firstCase
    let stringValue = enumValue.stringValue  // "Custom String for First Case"
    

    This approach is useful if the string representation of your enum cases is not just the case name or a simple string, but something more complex or dynamic.

Choose the method that best fits your requirements and the structure of your enum.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on November 17, 2023
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui swift August 31, 2024 @Published in SwiftUI

In SwiftUI, the @Published property wrapper is used in combination with the ObservableObject protocol to automatically announce changes to properties of a class. This allows SwiftUI views that depend on these properties to update automatically when the data changes.

question swiftui macos September 4, 2024 How to execute some code before app termination in a macOS app using SwiftUI?

To run some code before app termination in a macOS app using SwiftUI, the correct approach would involve placing the termination logic within a view, such as the ContentView. Here’s how you can do it:

question swiftui swift May 29, 2022 How to open a window in SwiftUI using NSWindowController?

Although many things in SwiftUI are idiomatic and straightforward, showing your view in a new window needs a bit of coding to do. Hence, this short post.