I have enum:
enum NewProgramDetails: String {
case Description = "Description", ToMode = "To Mode", From = "From", To = "To", Days = "Days"
static let allValues = [Description, ToMode, From, To, Days]
}
I want to use this enum to display in my cell depend on indexPath:
cell.textLabel.text = NewProgramDetails.ToMode
error: Cannot assign value of type 'ViewController.NewProgramDetails' to type 'String?'
How can I use enum values to assign it to label text as a string?
In swift 3, you can use this
var enumValue = Customer.Physics
var str = String(describing: enumValue)
Use the rawValue
of the enum:
cell.textLabel.text = NewProgramDetails.ToMode.rawValue
Other than using rawValue
,
NewProgramDetails.ToMode.rawValue // "To Mode"
you can also call String.init
to get the enum value's string representation:
String(NewProgramDetails.ToMode)
This will return "ToMode"
, which can be a little bit different from the rawValue
you assigned. But if you are lazy enough to not assign raw values, this String.init
method can be used!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With