Nekkoko Nabby
Nekkoko Nabby

Reputation: 57

Swift ENUM how to convert "rawValue" back to Enum case?

I have an enum:

  enum  switch : String {
      case on = "powerOn"
      case off = "powerOff"
      var japanswitch : String {
          case .on : return "onpu"
          case .off : return "offu"
      }
  }

In my code my function passed down "powerOn" as pure string parameter. The problem is some point of my function require to translate "powerOn" to japanswitch.

But problem is "powerOn" right now is nothing more than a mere String which has no relationship to japanswitch at all.

How do I translate "powerOn" to japanswitch? The desired result should be "onpu".

Upvotes: 2

Views: 4408

Answers (5)

henrique
henrique

Reputation: 1112

Your question is not very clear, would be helpful if you show us the code you're trying to run using this enum, but I think you can do something like this:

import Foundation 

enum SwitchType: String {
    case on = "powerOn"
    case off = "powerOff"

    var japaneseRepresentation : String {
        switch (self){
            case Toggle.on : return "onpu"
            case Toggle.off : return "offu"
            default: return Toggle.on.japaneseRepresentation
        }
    }

    init(japanString japanString: String) {
        switch(japanString) {
            case "onpu": self = Toggle.on
            case "offu": self = Toggle.off
            default: self = Toggle.on
        }
    }

}

let a = Toggle(rawValue: "powerOn")
print(a)
let b = Toggle(japanString: "onpu")
print(b)
print(a == b)

The benefit of doing this is that you can have a custom initializer to work with the same enum but initializing it from a Japaneses string.

Upvotes: 1

Amit Kumar
Amit Kumar

Reputation: 1

Try this one

enum  ABC : String {
      case on = "powerOn"
      case off = "powerOff"
      var japanswitch : String {
       return self == .on ? "onpu" : "offu"
      }
  }

  var a = ABC(rawValue: "powerOn")
  print(a!.japanswitch)

Upvotes: 1

Jyoti
Jyoti

Reputation: 16

enum Switch : String {
case on = "powerOn"
case off = "powerOff"

var japanswitch : String {
    switch self {
    case .on:
        return "onpu"
    case .off:
        return "offu"
    }
}}

You cannot use a case without a switch statement. So can try the above snippet. after defining enum as above, as per official Apple doc you can convert a string value to enum as follows.

If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value’s type (as a parameter called rawValue) and returns either an enumeration case or nil. You can use this initializer to try to create a new instance of the enumeration.

This example identifies japanswitch from Switch's raw value of "powerOn":

let japanswitchValue = Switch(rawValue: "powerOn")?.japanswitch

Upvotes: 0

Paulw11
Paulw11

Reputation: 114773

First you need to fix your code so that it compiles:

enum  Switch : String {
    case on = "powerOn"
    case off = "powerOff"
    var japanswitch : String {
        switch self {
        case .on : return "onpu"
        case .off : return "offu"
        }
    }
}

Then you can achieve what you are after using:

let japanese = Switch(rawValue:"powerOn")?.japanswitch

Note that japanese will be an optional; you need to decide how you will handle an invalid raw value.

Upvotes: 4

Jogendar Choudhary
Jogendar Choudhary

Reputation: 3494

Keyword 'switch' cannot be used as an identifier so you need to add some another keyword like below:

      enum  SwitchType : String {
            case on = "powerOn"
            case off = "powerOff"
            var japanswitch : String {
                switch self {
                case .on : return "onpu"
                case .off : return "offu"
                }
            }
        }

Upvotes: 1

Related Questions