0

I'm trying to get a default value for the enum so I can use it as a param. This code isn't working, but I'd like to get something like:

print("Param: \(Params.RCLoss.description)")

and the output should be:

Param: RC_LOSS_MAN

Here is the code:

enum Params {
  enum RCLoss: Int32, CustomStringConvertible {
    case disable = 0
    case enable = 1

    var description: String {
        return "RC_LOSS_MAN"
    }
  } 
}

I want to be able to pass this:

set(parameterType: Params.RCLoss.description, parameterValue: Params.RCLoss.enable)

which should correspond to these values being set:

set(parameterType: "RC_LOSS_MAN", parameterValue: 0)
Douglas Silva
  • 145
  • 1
  • 9
  • `.rawValue` should return you the `Int32`. Swift Guide: "You access the raw value of an enumeration case with its rawValue property." – keji Mar 05 '19 at 21:40
  • Possible duplicate of [What is the use of "static" keyword if "let" keyword used to define constants/immutables in swift?](https://stackoverflow.com/questions/34574876/what-is-the-use-of-static-keyword-if-let-keyword-used-to-define-constants-im) – John Montgomery Mar 05 '19 at 21:57
  • It is Swift naming convention to name all your enumeration types starting with an uppercase letter `enum RCLoss` – Leo Dabus Mar 05 '19 at 21:59

2 Answers2

1

It seems you want just

enum rcLoss: Int32 {
  case disable = 0
  case enable = 1 

  static var description: String {
    return "RC_LOSS_MAN"
  }
}

rcLoss is a type, description has to be static for you to be able to call rcLoss.description. And that means you cannot use CustomStringConvertible. You would use CustomStringConvertible to convert enum values to a String.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

From Swift Book - Enumerations:

You access the raw value of an enumeration case with its rawValue property.

set(parameterType: Params.rcLoss.description, parameterValue: Params.rcLoss.enable.rawValue)

If you can though I would use the enumeration as the type of the formal parameter so that someone can't pass an invalid value to that function. Also I'm assuming that there is a reason you have nested an enum inside of an otherwise empty enum...

keji
  • 5,947
  • 3
  • 31
  • 47
  • 1
    Given the name "Params," I suspect they're using the outer enum as a [poor man's namespace](https://stackoverflow.com/questions/39022242/difference-between-static-enum-and-static-struct) (and there are other properties that aren't relevant to the question). – John Montgomery Mar 05 '19 at 21:51