0

I have some enum for example

let variable: String = "SALESMAN"
enum Job
{
   SALESMAN = "Salesman"
   POSTMAN = "Postman"
}

and I'm getting from database String variable = "SALESMAN", how can get enum Job.SALESMAN using variable?

1
  • what did you try so far? what research did you do about enums? Commented Feb 9, 2016 at 14:04

1 Answer 1

2

You could do it like that:

let variable: String = "SALESMAN"
// Make your enum back value of String type
enum Job : String
{
    case SALESMAN = "Salesman"
    case POSTMAN = "Postman"
}

let job = Job(rawValue: variable.capitalizedString) //capitalizedString - change your variable to Salesman to match your enum back first case
// Note job is an optional value so you need to unwrap it if needed

print("\(job)")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.