-1

How to get the raw value from a enum passing the key value? Must work for any enum types working like an extension of enum types. Any mirror reflection, mappable or RawRepresentable solutions are welcome.

I would like something like this:

enum Test : String { 
     case One = "1" 
}

Test.rawValueFromKey(keyValue: "One") // Must return "1"
// I don't want the solution below, I must get the rawValue through the key name as String.
Test.One.rawValue

I do need to get the rawValue passing the name of the key as a String. Otherwise, I will need to make a switch or many if/else conditions. I have a big Enum and I don't want to check the string passed in a switch for example and pass Test.One.rawValue. I would like to get the rawValue directly through the key as String, just like in a dictionary.

I also don't want the code below, because I have a Enum of 40 items and I think it is ugly to make a switch of 40 items.

switch keyValue {
      case "One":
          return Test.One.rawValue
          break
}

I want something like this:

func rawValueFromKey (keyValue: String) -> Test {
    // code here to get the rawValue through the keyValue as String
    // return the proper Test Enum
}

I tried some possible solutions using Mirror reflection and enum iterations to find the rawValue through the keyValue but didn't work.

Please give the solution in both Swift 2 and 3.

Thanks

Arildo Junior
  • 796
  • 1
  • 8
  • 15
  • 1
    `Test.One.rawValue` – Willjay Jun 30 '17 at 02:53
  • 4
    https://stackoverflow.com/search?q=%5Bswift%5D+enum+rawValue I am afraid you haven't made the minimum amount of effort on your own. – El Tomato Jun 30 '17 at 03:01
  • [Swift Language Guide : Enumerations](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-ID145) Please read section *Raw Values*. – vadian Jun 30 '17 at 13:14
  • 1
    You guys did not understand my question, I just edited my question to make myself clear. I need to get the rawValue from the key name as String. Imagine you have a generic function where you pass Any Enum Type and a key name as String and it will get the raw value without any switch/if/else conditions. It gets directly through the key name. – Arildo Junior Jun 30 '17 at 18:06
  • "Any mirror reflection, mappable or RawRepresentable solutions are welcome" have you looked into a mirror solution? I suspect that's your only chance of doing this. – Connor Neville Jun 30 '17 at 18:15
  • 1
    FWIW, this smells of bad design implementation. I can't see a practical use case for this. – Connor Neville Jun 30 '17 at 18:16
  • I edited the question again. I tried some possible solutions using Mirror reflection and enum iterations to find the rawValue through the keyValue but didn't work. It is not a bad design implementation. I just want a shortcut to get the rawValue without switchs/if/else. – Arildo Junior Jun 30 '17 at 18:19

2 Answers2

0

As far as I know, you can't reflect on types so I think you will be able to achieve this only with CustomReflectable or maybe using localized strings for mapping the values/keys. Invariably you'll have to map somehow.

henrique
  • 1,072
  • 10
  • 17
0

Why not something like this? Sure you are just comparing Strings so it's potentially unsafe (if your enum would conform to CustomStringConvertible, etc), but you can't really avoid that.

I think CaseIterable is available only from Swift 4 though...

protocol KeyRepresentable: CaseIterable {
    
}

extension KeyRepresentable {
    static func fromKey(key: String) -> Self? {
        return Self
            .allCases
            .first { "\($0)" == key }
    }
}

Usage:

enum Option: Int, KeyRepresentable {
    case a = 1, b = 2
}

print(Option.fromKey(key: "a")?.rawValue)

MrKew
  • 333
  • 1
  • 14