0

I have an enum that looks like this:

enum IntteruptCause: UInt8 {
    case userDisplayDisengage = 0
    case userSteeringWheelDisengage = 1
    case positionDropout = 2
}

Currently the output from print() will give me the case name.

I would like to implment CustomStringConvertible and have it produce both the case name and the rawValue, e.g. the string User_Steering_Wheel_Disengage (1).

When I implement var description: String I have easy access to rawValue but I cannot seem to find out how to get the enum case name. Normally I would do String(describing: value) but that would create an infinite recursion if used in description implementation.

Is it possible to get the enum case name from within the description implementation?

3
  • 1
    I think hardcoding them is the best way there is at the moment... Commented Apr 6, 2020 at 10:02
  • Unrelated to your question, but you should conform to the Swift naming convention, which is lowerCamelCase for variables, including enum cases. Commented Apr 6, 2020 at 10:26
  • 1
    Possibly helpful: stackoverflow.com/q/24113126. Commented Apr 6, 2020 at 11:04

0

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.