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?
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?
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)")