0

I have some hardcoded string, based on that i have created switch case. But instead of string in switch case i am trying to create Enum for same. but i am not sure how to do that. I dont want to use default case.

Do i need to access with raw value or any other better way to do ?

enum screens: String {
        case faq = "faq", contactus = "contactus", termncondi = "termncondi", dashoboard = "dashoboard"
    }

    func deepLink(text: String) -> String {
            switch text {
                case "faq":
                    return (FAQ.localized())
                case "contactus":
                    return (Contactus.localized())
                case "termncondi":
                    return (Term.localized())
                case "dashoboard":
                    return (Dashboard.localized())
                default:
                    return ""
            }
        }
6
  • It depends. What is the text parameter? Commented Jun 25, 2020 at 10:00
  • text will be the one which i am using in switch case, for ex. "faq", "contactus"... Commented Jun 25, 2020 at 10:02
  • because..i dont want to use default case..and want to maintain string only at one place..so that i can use same functionality at multiple places Commented Jun 25, 2020 at 10:15
  • Of course but where does the string come from? Is it an input of a text field or a value of a JSON API or what? If the string is arbitrary there is no benefit to use an enum. Commented Jun 25, 2020 at 10:16
  • its fixed set of strings from response Commented Jun 25, 2020 at 10:17

2 Answers 2

2

You can simply define the enum and override the rawValue property like so:

enum Link {
    case faq
    case contact
    ...

    var rawValue: String {
        switch self {
            case .faq:
                return FAQ.localized()
            case .contact:
                return Contact.localized()
            default:
                return "Unknown case"
        }
    }
}

And get its rawValue.

let faqLink = Link.faq.rawValue

You can also perform a switch on an enum instance just like you do with a string.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for help..can you please suggest based on my updated question? I am confused in how to perform enum in switch case
I'm not sure why you would want to do it that way, just use the regular enum with the rawValue, like shown in my answer.
I am new to it..can you help me in using it in switch case? i am passing string to function
That won't even compile, because enum raw values need to be hardcoded constants. You'd need to manually define the rawValue property of the enum.
but here how it will know that faq has string "faq".. i am doing switch based on "faq" string
|
0

In order to get hardcoded values do something like:

public extension String {
    static func getScreens(name: DeepLink) -> String {
        return name.rawValue
    }

  public enum DeepLink: String {
    case faq = "faq"
    case contactus = "contactus"
    
    // and so on
}
}

1 Comment

how to add this in switch case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.